Compare running totals by order and by day
Your task
Return id, ordered_on, order_running_cents, and day_running_cents for completed orders. The order total stops at the current order in date and ID order. The day total includes all completed orders through the current date. Sort by date then ID.
A RANGE frame can include peers with the same sort value, while a ROWS frame advances with each individual row.
Sample data
shop_orders| id | customer_id | ordered_on | channel | status | total_cents |
|---|---|---|---|---|---|
| 201 | 1 | 2025-01-04 | online | completed | 2500 |
| 202 | 2 | 2025-01-10 | store | completed | 1800 |
| 203 | 2 | 2025-01-10 | store | completed | 3200 |
| 204 | 3 | 2025-01-18 | marketplace | refunded | 5000 |
| 205 | 4 | 2025-02-02 | store | completed | 2700 |
| 206 | 1 | 2025-02-04 | online | completed | 4000 |
| 207 | 5 | 2025-02-18 | online | cancelled | 2400 |
| 208 | 6 | 2025-02-28 | online | completed | 1700 |
| 209 | 4 | 2025-03-16 | online | completed | 2900 |
| 210 | 8 | 2025-03-09 | store | completed | 3100 |
Expected output
| id | ordered_on | order_running_cents | day_running_cents |
|---|---|---|---|
| 201 | 2025-01-04 | 2500 | 2500 |
| 202 | 2025-01-10 | 4300 | 7500 |
| 203 | 2025-01-10 | 7500 | 7500 |
| 205 | 2025-02-02 | 10200 | 10200 |
| 206 | 2025-02-04 | 14200 | 14200 |
| 208 | 2025-02-28 | 15900 | 15900 |
| 210 | 2025-03-09 | 19000 | 19000 |
| 209 | 2025-03-16 | 21900 | 21900 |
Show a hint
Use ROWS ordered by date and ID for the first sum, and RANGE ordered only by date for the second.
Window functions guide
Keep detail rows while summarizing
GROUP BY would return one row per genre. A window aggregate with PARTITION BY leaves every album in the result and gives each row its genre's average. This makes it easy to compare a row with its peers.
SELECT title, genre, price,
AVG(price) OVER (PARTITION BY genre) AS genre_average
FROM albums;Specify the window order and frame
The ORDER BY inside OVER defines the order of calculation. ROWS defines which neighboring rows contribute. This example adds the current sale to all earlier sales by ID. The last ORDER BY controls the order shown on screen.
SELECT id, quantity,
SUM(quantity) OVER (
ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS running_quantity
FROM sales
ORDER BY id;Rank first, then filter
Window results are calculated too late for the same SELECT's WHERE clause. Compute a rank in a CTE, then filter the named result. ROW_NUMBER assigns one position per row. RANK gives ties the same position and may leave gaps.
WITH ranked AS (
SELECT title, genre, ROW_NUMBER() OVER (
PARTITION BY genre ORDER BY price, id
) AS position
FROM albums
)
SELECT genre, title FROM ranked WHERE position = 1;Worked solution
SELECT id,ordered_on,SUM(total_cents) OVER (ORDER BY ordered_on,id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS order_running_cents,SUM(total_cents) OVER (ORDER BY ordered_on RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS day_running_cents FROM shop_orders WHERE status='completed' ORDER BY ordered_on,id;Press Control+Space for SQL suggestions. Use the arrow keys to select an option and Enter to insert it.
Results
Loading SQLite…
