Running Total Query in Oracle

Posted on 2003-12-02
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:

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
1   100   100
2   100   200
3   50    250
4   25    275

etc, etc.

Any ideas?
Question by:markag

Accepted Solution

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

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

Expert Comment

ID: 9860191
How about:

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

Expert Comment

ID: 9866362
Try this:

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

Love Analytics...


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Oracle Subquery bad Join 11 59
Convert Oracle data into XML document 2 54
query in Oracle forms Builder 2 39
Oracle RAC 12c 8 54
Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: 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…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

939 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

10 Experts available now in Live!

Get 1:1 Help Now