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

x
?
Solved

Running Total Query in Oracle

Posted on 2003-12-02
3
Medium Priority
?
858 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 1500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
Suggested Courses

886 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