I am trying to find the running sum of quantities across accounts over time. Ideally I want to sum the quantities of the current row and the most recent row for each account.
Given the following as example input data
| id | date | account_id | quantity |
|---|---|---|---|
| 1 | 01/01/2026 | 2 | 20 |
| 2 | 01/01/2026 | 3 | 30 |
| 3 | 02/01/2026 | 2 | 10 |
| 4 | 03/01/2026 | 1 | 5 |
| 5 | 04/01/2026 | 3 | 20 |
The output I desire would be:
| id | date | account_id | running_quantity |
|---|---|---|---|
| 1 | 01/01/2026 | 2 | 20 |
| 2 | 01/01/2026 | 3 | 50 |
| 3 | 02/01/2026 | 2 | 40 |
| 4 | 03/01/2026 | 1 | 45 |
| 5 | 04/01/2026 | 3 | 35 |
I have tried using a window function like:
select ... sum(quantity) over (partition by account_id order ... rows between unbounded preceding and current row) ...
However this provides only a running total of quantity on each by it's account id.
I have also tried using first_value/lag but again these each only associate with the current row's account id. I'm guessing this is not easily interpreted as a window function but I'm looking for any approach available.
If I use a sum on a row_number() based window, I receive row_number cannot be used here