Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Mikehjun
Starting Member
24 Posts |
Posted - 2013-03-27 : 11:26:40
|
Sum of the total miles for each quarter show like this,quarter total1 1232 3453 234 300however I want to get the result like this1q 2q 3q 4q123 345 23 300Any idea?my T-sql;SELECT DATEPART("Q", I.[ACTFINISHDATE]) AS QUARTER ,SUM(S.[Length]) AS "SUBTOTAL"FROM [CLGT].[INSPECTION] IINNER JOIN [LGDM].[SSGRAVITYMAIN] SON I.ENTITYUID = S.FACILITYIDWHERE I.STATUS = 'CLOSED' AND DATEPART("YY", I.[ACTFINISHDATE]) = '2012'GROUP BY DATEPART("Q", I.[ACTFINISHDATE]) |
|
UnemployedInOz
Yak Posting Veteran
54 Posts |
Posted - 2013-03-27 : 19:52:07
|
WITH PivotData AS(SELECT< grouping column >,< spreading column >,< aggregation column >FROM < source table >)SELECT < select list >FROM PivotDataPIVOT( < aggregate function >(< aggregation column >)FOR < spreading column > IN (< distinct spreading values >) ) AS P;orcreate table #test (ID int, Val int)goinsert into #test select 1, 123insert into #test select 2, 345insert into #test select 3, 23insert into #test select 4, 300goselect [1] as [1Q], [2] as [2Q], [3] as [3Q],[4] as [4Q]FROM #testPIVOT(sum(val) FOR ID IN ([1],[2],[3],[4]) ) AS P;-- Im sure you can figure out the rest |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-03-28 : 04:55:01
|
[code]SELECT SUM(CASE WHEN DATEPART("Q", I.[ACTFINISHDATE]) = 1 THEN S.[Length] ELSE 0 END) AS [1Q],SUM(CASE WHEN DATEPART("Q", I.[ACTFINISHDATE]) = 2 THEN S.[Length] ELSE 0 END) AS [2Q],SUM(CASE WHEN DATEPART("Q", I.[ACTFINISHDATE]) = 3 THEN S.[Length] ELSE 0 END) AS [3Q],SUM(CASE WHEN DATEPART("Q", I.[ACTFINISHDATE]) = 4 THEN S.[Length] ELSE 0 END) AS [4Q]FROM [CLGT].[INSPECTION] IINNER JOIN [LGDM].[SSGRAVITYMAIN] SON I.ENTITYUID = S.FACILITYIDWHERE I.STATUS = 'CLOSED' AND DATEPART("YY", I.[ACTFINISHDATE]) = '2012'[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|
|
|