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 |
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2012-10-29 : 08:41:40
|
| Hey guys I am hoping you will be able to help me I am trying to work out whether the net sales per month has either increased / decreased accordingly to last month’s previous figures In sql the equation would be “sum(February-January)/ January”My data in my query is displayed as the followingJanuary February march April etc etc 62382300 619495.21My query is as follows SELECT*FROM(SELECTRM_Code,ParentID,DATEADD(DAY,DATEDIFF(DAY,0,hst_date_processed),0) AS FirstDayOfMonth,SUM(Net_Sales) AS 'Total'FROM[FDMS].[dbo].[Fact_Financial_History]inner join Dim_Outlet oon hst_merchnum = FDMSAccountNo_First9WHEREhst_date_processed >= '20120101' AND hst_date_processed < '20130101'and RM_Account = 'y'GROUP BYRM_Code,o.ParentID,DATEADD(DAY,DATEDIFF(DAY,0,hst_date_processed),0)) s PIVOT( max(Total) FOR FirstDayOfMonth IN ([20120101],[20120201],[20120301],[20120401],[20120501],[20120601],[20120701],[20120801],[20120901],[20121001],[20121101],[20121201]))p |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-29 : 09:32:34
|
you dont need to pivot it for thisyou can keep the data as it is in table as rows and calculate the differencethe logic would be like ;With TempAS(SELECTRM_Code,ParentID,DATEADD(DAY,DATEDIFF(DAY,0,hst_date_processed),0) AS FirstDayOfMonth,SUM(Net_Sales) AS 'Total'FROM[FDMS].[dbo].[Fact_Financial_History]inner join Dim_Outlet oon hst_merchnum = FDMSAccountNo_First9WHEREhst_date_processed >= '20120101' AND hst_date_processed < '20130101'and RM_Account = 'y'GROUP BYRM_Code,o.ParentID,DATEADD(DAY,DATEDIFF(DAY,0,hst_date_processed),0))SELECT t1.*,t1.Total - COALESCE(t2.Total,0)/NULLIF(t2.Total,0) AS [Difference%],CASE WHEN t1.Total - COALESCE(t2.Total,0)=0 THEN 'No Change' WHEN t1.Total - COALESCE(t2.Total,0) > 0 THEN 'Up' ELSE 'Down'END AS DiffDirectionFROM Temp t1OUTER APPLY (SELECT Total FROM Temp WHERE RM_Code = t1.RM_Code AND ParentID = t1.Parent_ID AND FirstDayOfMonth = DATEADD(mm,-1,t1.FirstDayOfMonth) )t2 once you've happy with this then apply PIVOT over this as you did above------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2012-10-29 : 09:52:04
|
| hi visakh16I get the following error msg, when using your sql logic , any dieas why ?Msg 207, Level 16, State 1, Line 35Invalid column name 'Parent_ID'. |
 |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-10-29 : 09:54:21
|
| OUTER APPLY (SELECT Total FROM Temp WHERE RM_Code = t1.RM_Code AND ParentID = t1.ParentID AND FirstDayOfMonth = DATEADD(mm,-1,t1.FirstDayOfMonth) )t2--Chandu |
 |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2012-10-29 : 09:55:26
|
| Bandi i spotted that mistake and changed it and it still didnt work, after re-booting my sql it worked first time ! grrr |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-29 : 09:57:12
|
quote: Originally posted by masond Bandi i spotted that mistake and changed it and it still didnt work, after re-booting my sql it worked first time ! grrr
whats the error now?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-10-29 : 09:58:36
|
| Which error you are getting?? show us....--Chandu |
 |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2012-10-29 : 10:00:09
|
| The Error has been resolved now I am just having some server errors, due to us updating our tables |
 |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2012-10-29 : 10:13:23
|
| Hi Bandi / visakh16 I hope you don’t me asking, i just want to get my head around the logic you used in your query the query produces 6 columns Rm_Code ParentIdfirstdayofmonthtotal difference%DiffDirectionI understand the first 4 columns and i understand diffdirection column its just the difference % i cant seem to grasp what is the difference% calculating on ? I only ask, as i would have thought that the difference % should be worked out on latest month total – subtracted against previous month total, and that be the difference |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-29 : 10:16:36
|
quote: Originally posted by masond Hi Bandi / visakh16 I hope you don’t me asking, i just want to get my head around the logic you used in your query the query produces 6 columns Rm_Code ParentIdfirstdayofmonthtotal difference%DiffDirectionI understand the first 4 columns and i understand diffdirection column its just the difference % i cant seem to grasp what is the difference% calculating on ? I only ask, as i would have thought that the difference % should be worked out on latest month total – subtracted against previous month total, and that be the difference
it calculates the difference between each month against its previous month value------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2012-10-29 : 10:19:29
|
| See i thought that, but it doesnt seem to be working Please see below example RM_Code ParentID FirstDayOfMonth Total Difference% DiffDirectionR13 878233450880 2011-10-01 00:00:00.000 489701.87 NULL UpR13 878233450880 2011-11-01 00:00:00.000 532712.72 532711.72 UpR13 878233450880 2011-12-01 00:00:00.000 84656.13 84655.13 DownR13 878233450880 2012-01-01 00:00:00.000 361736.09 361735.09 UpR13 878233450880 2012-02-01 00:00:00.000 562981.60 562980.60 UpR13 878233450880 2012-03-01 00:00:00.000 990763.46 990762.46 UpR13 878233450880 2012-04-01 00:00:00.000 1031728.17 1031727.17 UpR13 878233450880 2012-05-01 00:00:00.000 1275166.24 1275165.24 UpR13 878233450880 2012-06-01 00:00:00.000 1243286.38 1243285.38 DownR13 878233450880 2012-07-01 00:00:00.000 1485590.96 1485589.96 UpR13 878233450880 2012-08-01 00:00:00.000 1164889.66 1164888.66 DownR13 878233450880 2012-09-01 00:00:00.000 674602.40 674601.40 Down |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-29 : 10:22:03
|
hmm...i think i got the issuetry this small modification;With TempAS(SELECTRM_Code,ParentID,DATEADD(DAY,DATEDIFF(DAY,0,hst_date_processed),0) AS FirstDayOfMonth,SUM(Net_Sales) AS 'Total'FROM[FDMS].[dbo].[Fact_Financial_History]inner join Dim_Outlet oon hst_merchnum = FDMSAccountNo_First9WHEREhst_date_processed >= '20120101' AND hst_date_processed < '20130101'and RM_Account = 'y'GROUP BYRM_Code,o.ParentID,DATEADD(DAY,DATEDIFF(DAY,0,hst_date_processed),0))SELECT t1.*,(t1.Total - COALESCE(t2.Total,0))/NULLIF(t2.Total,0) AS [Difference%],CASE WHEN t1.Total - COALESCE(t2.Total,0)=0 THEN 'No Change' WHEN t1.Total - COALESCE(t2.Total,0) > 0 THEN 'Up' ELSE 'Down'END AS DiffDirectionFROM Temp t1OUTER APPLY (SELECT Total FROM Temp WHERE RM_Code = t1.RM_Code AND ParentID = t1.Parent_ID AND FirstDayOfMonth = DATEADD(mm,-1,t1.FirstDayOfMonth) )t2 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
masond
Constraint Violating Yak Guru
447 Posts |
Posted - 2012-10-29 : 10:26:39
|
| visakh16 You are a star :) the amount of times you and others have bailed me out :) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-10-29 : 10:27:51
|
quote: Originally posted by masond visakh16 You are a star :) the amount of times you and others have bailed me out :)
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|