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
 Calculating Daily , MTD , YTD Percentage change

Author  Topic 

avmreddy17
Posting Yak Master

180 Posts

Posted - 2008-01-25 : 11:00:11
Here is the sample data .

Tradedate Type TotalOrdQty FillQty
2008-01-24 ATS 1392906.0 817965.0
2008-01-23 ATS 645306.0 469269.0
.... Goes on

Depending on the Type Column, I have to calculate the Percentage change in the TotalOrderQty and FillQty ( Daily , monthly , Yearly )

Is there a way to calculate the Percentage change with out using a
cursor.

Thanks
Venu

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-01-25 : 11:01:50
My first thought would be to use a case statement.
Go to Top of Page

avmreddy17
Posting Yak Master

180 Posts

Posted - 2008-01-25 : 11:33:33
I am using some thing like this..but I want to Generalise this so that I don't have to hardcode
the day as you can see in the below Qry..

SELECT dbo.ufn_PercentageChange (a.prev, b.curr, 0, 0)
FROM (SELECT SUM(TotalOrderQty) as [prev] FROM TradesONLINE..ManagementReport WHERE Day(TradeDate) = 23) as [a],(SELECT SUM(TotalOrderQty) as [curr] FROM TradesONLINE..ManagementReport WHERE Day(TradeDate) = 24) as [b]


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-25 : 11:39:07
Make it into a storeproc with two parameters @CurrentDay,@prevDay or single param @Day is if if you are always looking at consecutive days

CREATE PROC GetValues
@Day int
AS

SELECT dbo.ufn_PercentageChange (a.prev, b.curr, 0, 0)
FROM (SELECT SUM(TotalOrderQty) as [prev] FROM TradesONLINE..ManagementReport WHERE Day(TradeDate) = @Day-1) as [a],(SELECT SUM(TotalOrderQty) as [curr] FROM TradesONLINE..ManagementReport WHERE Day(TradeDate) = @Day) as [b]
GO
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-25 : 11:40:17
Also determine whether you are interested only in day portion else pass the params as date fields itself and compare against full value instead of DAY()
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-28 : 01:26:57
quote:
Originally posted by visakh16

Make it into a storeproc with two parameters @CurrentDay,@prevDay or single param @Day is if if you are always looking at consecutive days

CREATE PROC GetValues
@Day int
AS

SELECT dbo.ufn_PercentageChange (a.prev, b.curr, 0, 0)
FROM (SELECT SUM(TotalOrderQty) as [prev] FROM TradesONLINE..ManagementReport WHERE Day(TradeDate) = @Day-1) as [a],(SELECT SUM(TotalOrderQty) as [curr] FROM TradesONLINE..ManagementReport WHERE Day(TradeDate) = @Day) as [b]
GO


To make use of index on a date column, use


SELECT dbo.ufn_PercentageChange (prev, curr, 0, 0)
FROM
(
SELECT
SUM(case TradeDate>=dateadd(day,datediff(day,0,@Day)-1,0)
and TradeDate<dateadd(day,datediff(day,0,@Day),0) then TotalOrderQty else 0 end) as prev

SUM(case TradeDate>=dateadd(day,datediff(day,0,@Day),0)
and TradeDate<dateadd(day,datediff(day,1,@Day),0) then TotalOrderQty else 0 end) as curr

FROM TradesONLINE..ManagementReport
)
GO


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -