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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Quantity change between returned query rows

Author  Topic 

eliewadi
Starting Member

20 Posts

Posted - 2009-05-11 : 14:14:40
Hi,

I am trying to see if I can write a query that for each row it shows the quanity difference form the previous row.

query:
select file_date,sum(file_crq) qty from cust_fcst
group by file_date

returns:
file_date qty
---------- -----
2009-03-25 30500
2009-04-01 29000
2009-04-08 29000
2009-04-15 27000
2009-04-22 28500

What I want is to return an extra field that show the qty change from each previous row:

This is what i would like to get:
file_date qty Change
---------- ----- -------
2009-03-25 30500 0
2009-04-01 29000 -1500
2009-04-08 29000 0
2009-04-15 27000 -2000
2009-04-22 28500 1500

Thank you for any kind of help

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-05-11 : 14:24:17
Here's one way:

declare @cust_fcst table (file_date datetime, file_crq int)
insert @cust_fcst
select '2009-03-25', 30500 union all
select '2009-04-01', 29000 union all
select '2009-04-08', 29000 union all
select '2009-04-15', 27000 union all
select '2009-04-22', 28500

;with cte
as
(
select file_date
,sum(file_crq) qty
,row_number() over (order by file_date) as rn
from @cust_fcst
group by file_date
)
select a.file_date
,a.qty
,a.qty-isNull(b.qty, a.qty) as change
from cte a
left outer join cte b on b.rn = a.rn - 1


OUTPUT:
file_date qty change
----------------------- ----------- -----------
2009-03-25 00:00:00.000 30500 0
2009-04-01 00:00:00.000 29000 -1500
2009-04-08 00:00:00.000 29000 0
2009-04-15 00:00:00.000 27000 -2000
2009-04-22 00:00:00.000 28500 1500


Be One with the Optimizer
TG
Go to Top of Page

eliewadi
Starting Member

20 Posts

Posted - 2009-05-11 : 15:16:32
thank you this works! I will read about the CTE approach as I did not use it before. Is there another way?
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-05-11 : 15:30:53
>>Is there another way?
There always is

The one thing all set-based methods will have in common is a way to correlate a row in one instance of the data with the sequentially previous row of another instance of the table. That could be done using a self join with the table itself, two identical derived tables, 2 instances of a CTE, etc. The reason I used a CTE was that was a simple way to create a sequence number once and re-use it.

You could also use an OUTER APPLY to get the top 1 qty for each row ordered by file_date desc where the file_date is less than the "current" file_date.

Then of course there are non-set-based methods as well but they won't be as efficient.

Be One with the Optimizer
TG
Go to Top of Page

eliewadi
Starting Member

20 Posts

Posted - 2009-05-12 : 15:41:36

thank you for info, this was helpful
Go to Top of Page
   

- Advertisement -