SQL pivot嵌套

时间:2013-02-17 12:18:24

标签: sql-server excel tsql pivot

我使用的是Sql Server 2008 R2。我的工作是关于商业智能和报告。 现在我有一张桌子如下。

 declare @t table(  
 prod_type NVARCHAR(20),  
 WEEK   NVARCHAR(20),  
 COD_ACCT NVARCHAR(20),  
 BBH NVARCHAR(20)  
 );

 INSERT INTO @t VALUES ('Salary',   '41306',    '12313',    'Vikas'),  
 ('Salary', '41306',    '311',  'Ramesh'),  
 ('Salary', '41306',    '55',   'Vicky'),  
 ('Salary', '41306',    '44',   'Vicky'),  
 ('Salary', '41313',    '33',   'Vikas'),  
 ('Salary', '41313',    '22',   'Vikas'),  
 ('Salary', '41313',    '11',   'Ramesh'),  
 ('Salary', '41313',    '99',   'Vicky'),  
 ('Salary', '41320',    '88',   'Vicky'),  
 ('Salary', '41320',    '76',   'Vikas'),  
 ('Salary', '41320',    '545',  'Ramesh'),  
 ('Savings',    '41306',    '3',    'Vicky'),  
 ('Savings',    '41306',    '27',   'Ramesh'),  
 ('Savings',    '41306',    '324',  'Ramesh'),  
 ('Savings',    '41306',    '887',  'Vikas'),  
 ('Savings',    '41313',    '998',  'Vicky'),  
 ('Savings',    '41313',    '6754', 'Vikas'),  
 ('Savings',    '41313',    '3457', 'Ramesh'),  
 ('Savings',    '41313',    '5434', 'Vicky'),  
 ('Savings',    '41320',    '6554', 'Ramesh'),  
 ('Savings',    '41320',    '3322', 'Ramesh'),  
 ('Savings',    '41320',    '6542', 'Vikas')  
 ;

现在我希望以下列格式输出 -

           Salary                          ST            Savings              STT  GT

Row Labels  01-Feb-13 08-Feb-13 15-Feb-13      01-Feb-13 08-Feb-13 15-Feb-13    
Ramesh        1        1           1       3      2       1          2          5    8  
Vicky         2        1           1       4      1       2          0          3    7
Vikas         1        2           1       4      1       1          1          3    7 
Grand Total   4        4           3       11     4       4          3         11   22 

注意 - ST =工资总额,STT =保存总额,GT =总计(在Excel中以枢轴形式获得)

我现在所做的是将表格带入Excel并制作一个数据透视表,在Excel中的值为COUNT_ACCT,在行和Prod_type中为BBH,在Excel中为数据透视表中的列中的一周。

我可以在TSql中执行相同的操作。这将节省我所有的枢轴麻烦。

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以在SQL Server 2008 +中使用PIVOT函数和GROUPING SETS

您的代码与此类似:

select 
  case when bbh is null then 'Grand Total' else bbh end bbh,
  sum([Salary_2013-02-03]) [Salary_2013-02-03], 
  sum([Salary_2013-02-10]) [Salary_2013-02-10], 
  sum([Salary_2013-02-17]) [Salary_2013-02-17],
  sum([Salary_2013-02-03]+[Salary_2013-02-10]+[Salary_2013-02-17]) ST,
  sum([Savings_2013-02-03]) [Savings_2013-02-03],
  sum([Savings_2013-02-10]) [Savings_2013-02-10], 
  sum([Savings_2013-02-17]) [Savings_2013-02-17],
  sum([Savings_2013-02-03]+[Savings_2013-02-10]+[Savings_2013-02-17]) STT
from 
(
  select 
    prod_type +'_'+
    convert(varchar(10), cast(cast(week as int) as datetime), 120) week, 
    cod_acct, 
    bbh
  from t
) src
pivot
(
  count(cod_acct)
  for week in ([Salary_2013-02-03], [Salary_2013-02-10], 
               [Salary_2013-02-17], [Savings_2013-02-03],
               [Savings_2013-02-10], [Savings_2013-02-17])
) piv
group by grouping sets((bbh), ())

请参阅SQL Fiddle with Demo

这给出了结果:

|         BBH | SALARY_2013-02-03 | SALARY_2013-02-10 | SALARY_2013-02-17 | ST | SAVINGS_2013-02-03 | SAVINGS_2013-02-10 | SAVINGS_2013-02-17 | STT |
-----------------------------------------------------------------------------------------------------------------------------------------------------
|      Ramesh |                 1 |                 1 |                 1 |  3 |                  2 |                  1 |                  2 |   5 |
|       Vicky |                 2 |                 1 |                 1 |  4 |                  1 |                  2 |                  0 |   3 |
|       Vikas |                 1 |                 2 |                 1 |  4 |                  1 |                  1 |                  1 |   3 |
| Grand Total |                 4 |                 4 |                 3 | 11 |                  4 |                  4 |                  3 |  11 |