Solved

Cumulative totals in orale sql

Posted on 2011-02-23
2
561 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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Clone Oracle 12c Database 5 52
automatic email alert 1 50
Read only access to a Procedure in oracle? 4 65
Consolidating oracle query results to a single line 8 60
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…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to recover a database from a user managed backup
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

770 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