Author |
Topic |
nietzky
Yak Posting Veteran
75 Posts |
Posted - 2012-04-23 : 16:16:01
|
I am trying to include Totals row at the bottom of my grid.... I tried using C# code behind but I am hoping this can be done from within SQL. Any ideas on how to accomplish this? TYALTER PROCEDURE [dbo].[sp_Test]ASBEGINSELECTCOL_L,TEAM,S_LEVEL, ISNULL([1], 0) AS Jan, ISNULL([2], 0) AS Feb, ISNULL([3], 0) AS Mar, ISNULL([4], 0) AS Apr, ISNULL([5], 0) AS May, ISNULL([6], 0) AS Jun, ISNULL([7], 0) AS Jul, ISNULL( , 0) AS Aug, ISNULL([9], 0) AS Sep, ISNULL([10], 0) AS Oct, ISNULL([11], 0) AS Nov, ISNULL([12], 0) AS Dec FROM ( SELECT COL_L, TEAM, S_LEVEL, DATEPART(M, OPEN_T) AS AcqMonth, COL_L AS IMPACT, I_NO FROM dbo.ALL_YTD WHERE COL_L like 'P1%' AND SEV_CODE LIKE 'P1%'group by COL_L, TEAM,S_LEVEL, I_NO,DATEPART(M, OPEN_T))as pPIVOT ( COUNT(I_NO) for AcqMonth in ([1], [2], [3], [4], [5], [6], [7], , [9], [10], [11], [12]))as pvtENDdesired result:COL_LEVEL TEAM S_LEVEL Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov DecP1/2 TEAM1 Jesse James 1 1 0 0 0 0 0 0 0 0 0 0P1/3 TEAM2 Frank James 1 0 2 0 0 0 0 0 0 0 0 0P1/3 TEAM3 Clint Eastwood 0 0 0 1 0 0 0 0 0 0 0 0P1/3 TEAM4 John Wayne 0 0 1 0 0 0 0 0 0 0 0 0Totals 2 1 3 1 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-23 : 16:21:09
|
you must include the total via UNION ALL inside the main query over which you apply the PIVOT------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-23 : 19:37:57
|
see an illustration heredeclare @t table(id int identity(1,1),Category varchar(20),Yr int,Val int)insert @t (Category,Yr,Val)select 'Cat1',2011,15 union allselect 'Cat1',2010,22 union allselect 'Cat2',2011,30 union allselect 'Cat3',2011,19 union allselect 'Cat1',2011,32 union allselect 'Cat2',2010,44 union allselect 'Cat4',2011,30 union allselect 'Cat1',2010,14 union allselect 'Cat3',2011,35 select *from(select Category,cast(Yr as varchar(4)) AS Yr,Valfrom @tunion allselect Category,'Total',sum(Val)from @tgroup by Category)tpivot (sum(Val) for Category IN ([Cat1],[Cat2],[Cat3],[Cat4]))poutput-------------------------------------Yr Cat1 Cat2 Cat3 Cat42010 36 44 NULL NULL2011 47 30 54 30Total 83 74 54 30 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
nietzky
Yak Posting Veteran
75 Posts |
Posted - 2012-04-24 : 09:54:39
|
Great Thank you. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-24 : 21:38:45
|
wc------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|