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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How To PIVOT This?

Author  Topic 

Chamark
Starting Member

28 Posts

Posted - 2010-04-13 : 14:50:27
Beginner using MS-SQL2005

Would it be possible to PIVOT the results of this statement to have the months show as columns? Any help is greatly appreciated

Results should look like this
Jan Feb Mar Apr May Jun Jul . . .
Current Yr Actual xxx xxx xxx xxx xxx xxx xxx
Current Yr Target xxx xxx xxx xxx xxx xxx xxx
Current Yr Forecst xxx xxx xxx xxx xxx xxx xxx
.
.
.
SELECT
LEFT(DATENAME(month,YearMonth),3),k.[Actual_Month],[Target_Month],[Forecast_Month],
t.Actual_Month AS PYA
,k.[Actual_YTD]
,[Forecast_YTD]
,[Target_YTD]
,v.Actual_YTD AS [YTD PYA]
FROM [SPARS].[dbo].[t_kpi_Data] k
OUTER APPLY (Select Actual_Month

FROM [SPARS].[dbo].[t_kpi_Data]
WHERE YearMonth=DATEADD(yy,-1,k.YearMonth) AND DataSource_ID = k.DataSource_ID)t

OUTER APPLY (Select Actual_YTD
FROM [SPARS].[dbo].[t_kpi_Data]
WHERE YearMonth=DATEADD(yy,-1,k.YearMonth) AND DataSource_ID = k.DataSource_ID)v

WHERE k.YearMonth Between '01/01/2010' AND '12/31/2010'
AND k.DataSource_ID = '629'


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-13 : 23:50:22
its possible but you seem to have more than 1 month columns. so on which column basis you want to do pivot? some sample data of your query will also help

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -