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!