| Author |
Topic |
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2012-10-29 : 09:45:30
|
| Hey guys I am hoping you will be able to help mei am not sure if this viable, i tried to find some solutions online, but not getting much luck.What i am trying to achieve is create a pivot which brings in the financials for last 12months For eg, if a run the query today (October) it would give me September 2012- October 2011 financials If run the query in november2012-, it would provide me with October 2012 – November 2011My query is Declare @r12 datetimeset @r12 = dateadd(MM,-12,(select MAX(hst_date_processed) from dbo.Fact_Financial_History))SELECT ParentID,Parent_Name,RM_Code,[January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December]FROM (SELECT DISTINCT o.ParentID,o.Parent_Name,o.RM_Code,DATEADD(DAY,DATEDIFF(DAY,0,hst_date_processed),0) AS Months,--DATENAME(Month, hst_date_processed) MonthsSum (Net_Sales) AS 'Total'FROM [FDMS].[dbo].[Fact_Financial_History] inner join Dim_Outlet o on hst_merchnum = FDMSAccountNo_First9WHERE hst_date_processed >@r12and RM_Account = 'y'GROUP BYo.ParentID,o.Parent_Name,o.RM_Code, hst_date_processed) AS pPIVOT (sum(Total) FOR Months IN ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December]))AS pvtHoping that you can help |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-29 : 09:55:26
|
[code]SELECT ParentID,Parent_Name,RM_Code,[1],[2],[3],[4],[5],[6],[7], ,[9],[10],[11],[12]FROM (SELECT ROW_NUMBER() OVER (PARTITION BY o.ParentID,o.Parent_Name,o.RM_CodeORDER BY DATEADD(DAY,DATEDIFF(DAY,0,hst_date_processed),0) DESC) AS Seq,o.ParentID,o.Parent_Name,o.RM_Code,--DATENAME(Month, hst_date_processed) MonthsSum (Net_Sales) AS 'Total'FROM [FDMS].[dbo].[Fact_Financial_History] inner join Dim_Outlet o on hst_merchnum = FDMSAccountNo_First9WHERE hst_date_processed >@r12and RM_Account = 'y'GROUP BYo.ParentID,o.Parent_Name,o.RM_Code, DATEADD(DAY,DATEDIFF(DAY,0,hst_date_processed),0) ) AS pPIVOT (sum(Total) FOR Seq IN ([1],[2],[3],[4],[5],[6],[7], ,[9],[10],[11],[12]))AS pvt[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2012-10-29 : 10:03:44
|
| Hi visakh16 Thank you for your response is there anyway that i could reference the months instead of numbers for eg 1 = October 20112=November 201112 = September 2012 And these months would change based on my declaration (as seen below )Declare @r12 datetimeset @r12 = dateadd(MM,-12,(select MAX(hst_date_processed) from dbo.Fact_Financial_History)) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-29 : 10:13:18
|
quote: Originally posted by masond Hi visakh16 Thank you for your response is there anyway that i could reference the months instead of numbers for eg 1 = October 20112=November 201112 = September 2012 And these months would change based on my declaration (as seen below )Declare @r12 datetimeset @r12 = dateadd(MM,-12,(select MAX(hst_date_processed) from dbo.Fact_Financial_History))
thats why you could put only numbersyou cant change the column names on the fly unless you use dynamic sqllike thishttp://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|