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

时间:2015-07-06 16:54:33

标签: sql-server database sql-server-2008

嘿那里我正在寻找基于下表的具体输出

数据库表

╔══════════════════════════════════════════════════════════════════════╗
║ 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     ║
╚═════════════════════════════════════════════════════════════════════════════╝

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

1 个答案:

答案 0 :(得分:0)

就像@SQLPolice所说,你可以用汇总做这样的事情,但我不能真的推荐它。它变得混乱,难以维护。这应该在数据库之外完成:

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

我的假设是,总计实际上应该是行的总计,并且仅在摘要级别上显示,而不是全部显示,并且实际上并不存在于数据中。

这里的技巧是分组(字段),它告诉行是详细行还是摘要行,汇总在更高级别创建摘要。

这是SQL Fiddle可以玩的。