Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating cumulative figures by month 1

Status
Not open for further replies.

robo100

Programmer
Jun 19, 2003
30
GB
I have the following in a table

Store Period Year Date Value
1 1 2004 1/1/04 100
1 2 2004 1/2/04 200
1 3 2004 1/3/04 100
2 1 2004 1/1/04 50
2 2 2004 1/2/04 75

I want to write a view to show the cumulative value by month so the data would look as follows.

Store Period Year Date Cum_Value
1 1 2004 1/1/04 100
1 2 2004 1/2/04 300
1 3 2004 1/3/04 400
2 1 2004 1/1/04 50
2 2 2004 1/2/04 125

Any help would be appreciated as I am struggling with this.

Thanks in advance.
 
One possibility is use an inline select statement:

Code:
select store, period, year, date, amount, 
(select sum(amount) from amounts a2
where a1.store = a2.store
and a2.date <= a1.date) cum_amount
from amounts a1
 
Just to perhaps "gild the lily" that Dagon so well presented, you will probably want to add:
Code:
...
order by store, period
...since you cannot rely upon the "physical" order of rows in an Oracle table.

Cheers,

[santa]Mufasa
(aka Dave of Sandy, Utah, USA @ 17:31 (23Sep04) UTC (aka "GMT" and "Zulu"), 10:31 (23Sep04) Mountain Time)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top