Solved

Cumulative totals in orale sql

Posted on 2011-02-23
2
551 Views
Last Modified: 2012-06-21
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
Comment
Question by:subratoc
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 total points
ID: 34959902
select col1, col2, sum(col2*col1) over (order by col1)  
  from yourtable
order by col1
0
 

Author Comment

by:subratoc
ID: 34965876
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now