1

I am using Sql Server 2008 R2. My work is regarding Business Intelligence and Reporting. Now I have a table as below.

 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')  
 ;

Now I want output in following format-

           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 

Note- ST= Salary Total ,STT= Saving Total ,GT= Grand Total (as Obtained in Pivot in Excel)

What I do now is take table into Excel and Make a pivot as such with COUNT of COD_ACCT in Values in Excel and BBH in Row and Prod_type and week in Columns in Pivot in Excel.

Can I do that same in TSql. It will save me all of that Pivot hassle.

Thanks!

4

1 に答える 1

1

You can use the PIVOT function as well as GROUPING SETS in SQL Server 2008+.

Your code would be similar to this:

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), ())

See SQL Fiddle with Demo.

This gives the result:

|         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 |
于 2013-02-17T15:13:55.910 に答える