Running Total by Group SQL (Oracle)

I have a table in Oracle db that has the following fields of interest: Location, Product, Date, Amount. I would like to write a request that will receive the total amount by location, product and date. I will give an example table in which I would like to get the results.

I can get the total, but I can’t get it until reset when I reach the new location / product. This is the code that I still have, any help would be greatly appreciated, I have the feeling that 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       |
+----------+---------+-----------+------------+------------+
+4
source share
3 answers

And I think I understood that.

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

SQL Oracle 10g, .

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
+1

I could type the whole answer or send you to where I recognized it. :)

Check it out, it explains exactly what you are trying to do.

http://www.codeproject.com/Articles/300785/Calculating-simple-running-totals-in-SQL-Server

-3
source

Source: https://habr.com/ru/post/1529872/


All Articles