Running Total by Group SQL (Oracle)

45,436

Solution 1

Ah, I think I have figured it out.

select a.*, sum(Amount) over (partition by Location, Product order by Date) as Running_Amt
from Example_Table a

Solution 2

from Advanced SQL Functions in Oracle 10g book, it has this example.

SELECT dte "Date", location, receipts,
SUM(receipts) OVER(ORDER BY dte
ROWS BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW) "Running total"
FROM store
WHERE dte < '10-Jan-2006'
ORDER BY dte, location
Share:
45,436
user1723699
Author by

user1723699

Updated on July 21, 2022

Comments

  • user1723699
    user1723699 almost 2 years

    I have a table in an Oracle db that has the following fields of interest: Location, Product, Date, Amount. I would like to write a query that would get a running total of amount by Location, Product, and Date. I put an example table below of what I would like the results to be.

    I can get a running total but I can't get it to reset when I reach a new Location/Product. This is the code I have thus far, any help would be much appreciated, I have a feeling this is a simple fix.

    select a.*, sum(Amount) over (order by Location, Product, Date) as Running_Amt
    from Example_Table a
    
    +----------+---------+-----------+------------+------------+
    | Location | Product | Date      | Amount     |Running_Amt |
    +----------+---------+-----------+------------+------------+
    | A        | aa      | 1/1/2013  | 100        | 100        |
    | A        | aa      | 1/5/2013  | -50        | 50         |
    | A        | aa      | 5/1/2013  | 100        | 150        |
    | A        | aa      | 8/1/2013  | 100        | 250        |
    | A        | bb      | 1/1/2013  | 500        | 500        |
    | A        | bb      | 1/5/2013  | -100       | 400        |
    | A        | bb      | 5/1/2013  | -100       | 300        |
    | A        | bb      | 8/1/2013  | 250        | 550        |
    | C        | aa      | 3/1/2013  | 550        | 550        |
    | C        | aa      | 5/5/2013  | -50        | 600        |
    | C        | dd      | 10/3/2013 | 999        | 999        |
    | C        | dd      | 12/2/2013 | 1          | 1000       |
    +----------+---------+-----------+------------+------------+
    
  • Joe Trader
    Joe Trader about 10 years
    You don't find the article that explains how to partition the data useful? Although the answer is partitioning.
  • crennie
    crennie about 10 years
    I didn't downvote this answer, but I think there is definitely room for improvement. Maybe consider adding more context/content - there seems to be a consensus on SO that links alone are not sufficient for a good answer.
  • crennie
    crennie about 10 years
    As an aside, this link may have been more appropriate as a comment instead of an answer... but unfortunately you need 50 rep for that. If you're like me and you thought that was lame, then check out this question about it on meta, which gives the limitation some motivation, and also suggests some ways to write a great answer :D
  • a_horse_with_no_name
    a_horse_with_no_name about 10 years
    The linked article is for SQL Server. Oracle has a much more efficient way of doing this using a window function.
  • Joe Trader
    Joe Trader about 9 years
    Thanks for taking the time to criticize my answer which solved the problem. Many thanks for mentioning that oracle has a more efficient way but not bothering to answer the question. It would have been more helpful if you actually provided some instruction on your efficient way. The answer I referenced works in oracle the same way it does in SQL Server.