Sql server 如何以特定格式显示表格数据

Sql server 如何以特定格式显示表格数据,sql-server,database,sql-server-2008,Sql Server,Database,Sql Server 2008,嘿,我正在根据下表寻找一个特定的输出 数据库表 ╔══════════════════════════════════════════════════════════════════════╗ ║ Department Division Name Level1 Level2 Level3 GrandTotal ║ ╠══════════════════════════════════════════════════════════════════════╣ ║ Insur

嘿,我正在根据下表寻找一个特定的输出

数据库表

╔══════════════════════════════════════════════════════════════════════╗
║ Department  Division      Name   Level1    Level2  Level3 GrandTotal ║
╠══════════════════════════════════════════════════════════════════════╣
║ Insurance   Distribution  John                1               3      ║
║ Insurance   Distribution  Mark     1                          3      ║
║ Insurance   Distribution  Joe      1                          3      ║
║ Marketing   Distribution  Rob                         1       2      ║
║ Marketing   Distribution  Sam      1                          2      ║
║ Claims      Solutions     Bob      1                          3      ║
║ Claims      Solutions     Tom      1                          3      ║
║ Claims      Solutions     Gin                         1       3      ║
╚══════════════════════════════════════════════════════════════════════╝
我想要的输出是

╔═════════════════════════════════════════════════════════════════════════════╗
║     Department  Division     Name     Level1  Level2     Level3  GrandTotal ║
╠═════════════════════════════════════════════════════════════════════════════╣
║     Insurance   Distributon                                           3     ║
║                              John               1                     1     ║
║                              Mark        1                            1     ║
║                              Joe         1                            1     ║
║     Marketing  Distribution                                           2     ║
║                              Rob                            1         1     ║
║                              Sam         1                            1     ║
║     Claims     Solution                                               3     ║
║                              Bob         1                            1     ║
║                              Tom         1                            1     ║
║                              Gin                            1         1     ║
╚═════════════════════════════════════════════════════════════════════════════╝

首先,如果有这种类型的表输出的名称,我会道歉,因为我不知道它,并会使标题更好。我有一个想法,使用游标遍历每一行,并将部门和部门存储在一个变量中,每次都检查它是否相同,如果相同,则清空部门和部门,直到它们发生变化。但我希望有一个更简单的方法来做到这一点。任何帮助都将不胜感激。

就像@SQLPolice所说的,你可以用rollup做类似的事情,但我不能真正推荐它。它变得凌乱,难以维护。这实际上应该在数据库之外完成:

select 
  case when grouping(name) = 1 then department end as department, 
  case when grouping(name) = 1 then division end as division, 
  name,
  case when grouping(name) = 0 then sum(isnull(level1,0)) end as level1,
  case when grouping(name) = 0 then sum(isnull(level2,0)) end as level2,
  case when grouping(name) = 0 then sum(isnull(level3,0)) end as level3,
  case when grouping(name) = 1 then sum(isnull(level1,0)) + sum(isnull(level2,0)) + sum(isnull(level3,0)) end as GrandTotal
from 
  table1
group by
  department, 
  division, 
  rollup(name)
order by
  table1.department,
  table1.division,
  grouping(name) desc
我的假设是,总计实际上应该是行的总计,并且只在摘要级别显示,而不是所有行,并且在数据中并不存在

这里的诀窍是分组(字段),它告诉我们行是详细信息行还是摘要行,然后汇总在更高级别上创建摘要


让我们一起玩吧。

快点。。。您可以使用
汇总
执行此操作。我现在没有时间写一个全面的答案,但是也许你可以查一下,或者其他人会写一个答案。你也可以使用UNION ALL和一个不出现在选择列表中的人工按列排序。感谢SQLPolice和Tab Alleman的建议感谢JamesZ的回复和帖子我将尝试一下,看看它是如何进行的。JamesZ这个案例中的快速问题在测试表中只有3个级别,但是如果有超过15个级别或者级别数未知,您会如何更改上面的内容,因为案例会很长。如果有办法的话。thanks@JayJay在select中拥有未知数量的列总是令人讨厌的事情,最终您将创建动态SQL,而维护动态SQL则更不有趣(甚至在6个月后理解)。你真的应该尝试在数据库之外做类似的事情。JamesZ再次感谢,它完全按照我需要的方式工作。还要感谢所有提出建议的人。