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 2000 Forums
 SQL Server Development (2000)
 Opposite of Running Total (Running Difference?)

Author  Topic 

stannius
Starting Member

10 Posts

Posted - 2004-02-13 : 14:07:27
I have a database which contains a bunch of year-to-date revenue data at the end of some(though often not all) months. For example:

Member report_date_from report_date_to ytd_revenue
100 2002-01-01 00:00:00.000 2002-04-30 00:00:00.000 9492891.2500
100 2002-01-01 00:00:00.000 2002-08-31 00:00:00.000 11514479.2500
100 2002-01-01 00:00:00.000 2002-09-30 00:00:00.000 12132443.2500
100 2002-01-01 00:00:00.000 2002-10-31 00:00:00.000 12408736.2500
100 2002-01-01 00:00:00.000 2002-11-30 00:00:00.000 12396812.2500
100 2002-01-01 00:00:00.000 2002-12-31 00:00:00.000 12710908.2500

This is an example of a subset of the data; there are other members in there too.

What I want to do is figure out another column, period_revenue, which is the difference between the ytd_revenue of a record and the most recent record before that. I have found examples of how to calculate the running total but not how to subtract it like this.

Thanks,
steve

(xposted on t-sql)

dsdeming

479 Posts

Posted - 2004-02-17 : 15:02:23
It may not be the most elegant solution, but you could take the difference between two running totals, one of which includes rows where some key is <= the current one and another which only includes rows where the key is < the current one.

Dennis
Go to Top of Page

stannius
Starting Member

10 Posts

Posted - 2004-02-17 : 15:13:04
quote:
Originally posted by dsdeming

It may not be the most elegant solution, but you could take the difference between two running totals, one of which includes rows where some key is <= the current one and another which only includes rows where the key is < the current one.

Dennis



Wouldn't that just tell me the value of the current row?
steve
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-02-17 : 15:14:01
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=32521
Go to Top of Page
   

- Advertisement -