Solved

Running Total Query in Oracle

Posted on 2003-12-02
3
849 Views
Last Modified: 2008-03-17
8i, Standard, no Analytic functions available....

I need to calculate a running total column which would be the sum of a column for all preceding rows including the current row. for example, if my data was:

ID VAL
1   100
2   100
3   50
4  25

and I was doing a running total on VAL, Ordered By ID, then my result set would be
ID VAL RUNNING_TOTAL
1   100   100
2   100   200
3   50    250
4   25    275

etc, etc.

Any ideas?
0
Comment
Question by:markag
3 Comments
 
LVL 2

Accepted Solution

by:
IanBlundell earned 500 total points
ID: 9860177
Given a table called exp_test, try something like this:

select      it1.id
,            it1.val
,            (select      sum(it2.val)
             from      exp_test it2
             where      it2.id <= it1.id)            
from exp_test it1
0
 
LVL 15

Expert Comment

by:andrewst
ID: 9860191
How about:

select id, val, (select sum(val) from mytable t2 where t2.id <= t1.id) running_total
from mytable t1
order by id;
0
 
LVL 13

Expert Comment

by:riazpk
ID: 9866362
Try this:


select id,val, sum(val) over (order by id) Running_Total
from mytable
order by id
/



Love Analytics...


Riaz
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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

809 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