Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 591
  • Last Modified:

Cumulative totals in orale sql

Is it possible to calculate cumulatively in oracle, i.e. using the results of the last row for calculating the values in the current row?
For example I want my query to return the following:
Month      col1      _      col2      _      col3
jan      1      _      100      _      100 * 1
feb      2      _      200      _      200 * 2 + 100
mar      3      _      300      _      300 * 3 + 500
apr      4      _      400      _      400 * 4 + 1400
may      5      _      500      _      500 * 5 + 5500

In col3 above, for feb I want to use the result returned for jan (100 * 1=100), for mar I am using the result returned for feb(200 * 2 + 100=500) and so on.

Thanks in advance.
0
subratoc
Asked:
subratoc
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
select col1, col2, sum(col2*col1) over (order by col1)  
  from yourtable
order by col1
0
 
subratocAuthor Commented:
Sorry, I should have given a slightly different example. I meant to say that I want to use the previous value of column3 to derive the current value of column3. Like using the LAG function but on the analytically derived column itself. The revised example would be:
Month      col1      _      col2      _      col3
jan      1      _      100      _      100 * 1
feb      2      _      200      _      100 * 2 + 200
mar      3      _      300      _      400 * 3 + 300
apr      4      _      400      _      1500 * 4 + 400
may      5      _      500      _      6400 * 5 + 500
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now