SQL Oracle Running Total

Hello Experts Exchange
I have a table with Year, Income_Total.

I want a Running total on my select statement.

So it should look like this.

Year                  Income_Total                     Running_Total
--------------         ---------------------                   ----------------------
2013                  2000                                    2000
2014                  1500                                    3500
2015                  500                                      4000

Can you give me the SQL Statement to achive this please?

Regards

SQLSearcher
SQLSearcherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

slightwv (䄆 Netminder) Commented:
try this:
select year, income_total, sum(income_total) over(order by year) running_total from some_table;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulfreelancerCommented:
-- & add
order by year

or at least some logical order or the running sum can be illogical
:-)
0
SQLSearcherAuthor Commented:
Thank you very much.

I ended up with a query like this;

select year, AP_Period_Number, Sum(income_total) as Income_Total,Sum(sum(income_total)) over(order by year, AP_Period_Number) as running_total
from LEGACY_INCOME_SUMMARY
Where Year = 2014
Group by Year,AP_Period_Number
Union All
select year, AP_Period_Number, Sum(income_total) as Income_Total,Sum(sum(income_total)) over(order by year, AP_Period_Number) as running_total
from LEGACY_INCOME_SUMMARY
Where Year = 2015
Group by Year,AP_Period_Number
order by Year,AP_Period_Number;
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

slightwv (䄆 Netminder) Commented:
I didn't test this but try to simplify what you have.  You shouldn't need to hit the table twice.

select year, AP_Period_Number, income_total,Sum(income_total) over(order by year, AP_Period_Number) running_total 
from
(
select year, AP_Period_Number, Sum(income_total) as Income_Total
from LEGACY_INCOME_SUMMARY
Where Year in (2014, 2015)
Group by Year,AP_Period_Number
)
order by Year,AP_Period_Number; 

Open in new window

0
PortletPaulfreelancerCommented:
perhaps they are wanting running total to reset at beginning of each year, so need to PARTITION as well as order

e.g.

select
year, month, val, sum(val) over(partition by year order by month)
from (
  select year, month, sum(val) val
  from table1
  group by year, month
  )
 
| YEAR | MONTH | VAL | SUM(VAL)OVER(PARTITIONBYYEARORDERBYMONTH) |
|------|-------|-----|-------------------------------------------|
| 2014 |     1 | 100 |                                       100 |
| 2014 |     2 | 100 |                                       200 |
| 2014 |     3 | 100 |                                       300 |
| 2014 |     4 | 100 |                                       400 |
| 2014 |     5 | 100 |                                       500 |
| 2014 |     6 | 100 |                                       600 |
| 2015 |     1 | 100 |                                       100 |
| 2015 |     2 | 100 |                                       200 |
| 2015 |     3 | 100 |                                       300 |
| 2015 |     4 | 100 |                                       400 |
| 2015 |     5 | 100 |                                       500 |
| 2015 |     6 | 100 |                                       600 |

CREATE TABLE Table1
    (YEAR int, MONTH int, VAL int)
;

INSERT ALL 
    INTO Table1 ("YEAR", "MONTH", "VAL")
         VALUES (2014, 1, 100)
    INTO Table1 ("YEAR", "MONTH", "VAL")
         VALUES (2014, 2, 100)
    INTO Table1 ("YEAR", "MONTH", "VAL")
         VALUES (2014, 3, 100)
    INTO Table1 ("YEAR", "MONTH", "VAL")
         VALUES (2014, 4, 100)
    INTO Table1 ("YEAR", "MONTH", "VAL")
         VALUES (2014, 5, 100)
    INTO Table1 ("YEAR", "MONTH", "VAL")
         VALUES (2014, 6, 100)
    INTO Table1 ("YEAR", "MONTH", "VAL")
         VALUES (2015, 1, 100)
    INTO Table1 ("YEAR", "MONTH", "VAL")
         VALUES (2015, 2, 100)
    INTO Table1 ("YEAR", "MONTH", "VAL")
         VALUES (2015, 3, 100)
    INTO Table1 ("YEAR", "MONTH", "VAL")
         VALUES (2015, 4, 100)
    INTO Table1 ("YEAR", "MONTH", "VAL")
         VALUES (2015, 5, 100)
    INTO Table1 ("YEAR", "MONTH", "VAL")
         VALUES (2015, 6, 100)
SELECT * FROM dual
;

Open in new window

http://sqlfiddle.com/#!4/3ac60/1
0
slightwv (䄆 Netminder) Commented:
>>so need to PARTITION as well as order

Probably.  Thanks for catching that!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.