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
 % increase /decrease calculation

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 following
January February march April etc etc
62382300 619495.21



My query is as follows

SELECT
*
FROM
(
SELECT
RM_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 o
on hst_merchnum = FDMSAccountNo_First9
WHERE
hst_date_processed >= '20120101' AND hst_date_processed < '20130101'
and RM_Account = 'y'

GROUP BY
RM_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 this

you can keep the data as it is in table as rows and calculate the difference

the logic would be like

;With Temp
AS
(
SELECT
RM_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 o
on hst_merchnum = FDMSAccountNo_First9
WHERE
hst_date_processed >= '20120101' AND hst_date_processed < '20130101'
and RM_Account = 'y'

GROUP BY
RM_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 DiffDirection
FROM Temp t1
OUTER 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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

masond
Constraint Violating Yak Guru

447 Posts

Posted - 2012-10-29 : 09:52:04
hi visakh16

I get the following error msg, when using your sql logic ,
any dieas why ?
Msg 207, Level 16, State 1, Line 35
Invalid column name 'Parent_ID'.
Go to Top of Page

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
Go to Top of Page

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
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-10-29 : 09:58:36
Which error you are getting?? show us....

--
Chandu
Go to Top of Page

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
Go to Top of Page

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
ParentId
firstdayofmonth
total
difference%
DiffDirection

I 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
Go to Top of Page

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
ParentId
firstdayofmonth
total
difference%
DiffDirection

I 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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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% DiffDirection
R13 878233450880 2011-10-01 00:00:00.000 489701.87 NULL Up
R13 878233450880 2011-11-01 00:00:00.000 532712.72 532711.72 Up
R13 878233450880 2011-12-01 00:00:00.000 84656.13 84655.13 Down
R13 878233450880 2012-01-01 00:00:00.000 361736.09 361735.09 Up
R13 878233450880 2012-02-01 00:00:00.000 562981.60 562980.60 Up
R13 878233450880 2012-03-01 00:00:00.000 990763.46 990762.46 Up
R13 878233450880 2012-04-01 00:00:00.000 1031728.17 1031727.17 Up
R13 878233450880 2012-05-01 00:00:00.000 1275166.24 1275165.24 Up
R13 878233450880 2012-06-01 00:00:00.000 1243286.38 1243285.38 Down
R13 878233450880 2012-07-01 00:00:00.000 1485590.96 1485589.96 Up
R13 878233450880 2012-08-01 00:00:00.000 1164889.66 1164888.66 Down
R13 878233450880 2012-09-01 00:00:00.000 674602.40 674601.40 Down
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-29 : 10:22:03
hmm...i think i got the issue

try this small modification


;With Temp
AS
(
SELECT
RM_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 o
on hst_merchnum = FDMSAccountNo_First9
WHERE
hst_date_processed >= '20120101' AND hst_date_processed < '20130101'
and RM_Account = 'y'

GROUP BY
RM_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 DiffDirection
FROM Temp t1
OUTER 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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 :)
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -