Running total per customer
HardWindow functionsE-commerce
The problem
For delivered orders only, show customer_id, order_id, amount and a running_total of that customer's delivered amounts so far.
The running total restarts for each customer and follows order_date (then order_id). Sort the output by customer_id, order_date, order_id.
Learn this in the course: Window functions (video and notes)
Tables · Online shop
Click a table or column name to insert it into your query.
- INTEGER · primary key
- TEXT
- TEXT · can be NULL
- DATE
Sample rows (10 total)
| customer_id | name | city | signup_date |
|---|---|---|---|
| 1 | Aarav Mehta | Mumbai | 2025-01-10 |
| 2 | Diya Nair | Bengaluru | 2025-01-18 |
| 3 | Kabir Singh | Delhi | 2025-02-02 |
| 4 | Meera Iyer | Chennai | 2025-02-14 |
| 5 | Rohan Das | Kolkata | 2025-03-01 |
- INTEGER · primary key
- INTEGER · → customers
- DATE
- TEXT · delivered | cancelled | returned | pending
- INTEGER · rupees
Sample rows (20 total)
| order_id | customer_id | order_date | status | amount |
|---|---|---|---|---|
| 1 | 1 | 2025-05-01 | delivered | 1200 |
| 2 | 1 | 2025-05-15 | delivered | 800 |
| 3 | 1 | 2025-06-02 | cancelled | 500 |
| 4 | 2 | 2025-05-03 | delivered | 2500 |
| 5 | 2 | 2025-05-20 | delivered | 450 |
Ctrl/⌘ + Enter runs the query. Table and column names are suggested as you type. Press Esc then Tab to leave the editor with the keyboard.
Expected output (14 rows)
| customer_id | order_id | amount | running_total |
|---|---|---|---|
| 1 | 1 | 1200 | 1200 |
| 1 | 2 | 800 | 2000 |
| 2 | 4 | 2500 | 2500 |
| 2 | 5 | 450 | 2950 |
| 2 | 6 | 1800 | 4750 |
| 3 | 8 | 300 | 300 |
| 4 | 10 | 1500 | 1500 |
| 4 | 11 | 1500 | 3000 |
| 4 | 12 | 600 | 3600 |
| 5 | 14 | 700 | 700 |
| 6 | 15 | 3200 | 3200 |
| 6 | 16 | 400 | 3600 |
| 6 | 17 | 1100 | 4700 |
| 7 | 19 | 950 | 950 |
Row order matters for this question. Numbers are compared with a tiny tolerance; text must match exactly.
Stuck?
Stored on this device only. Your latest query and “solved” status are remembered in this browser only. They don’t sync between devices, and clearing your browser data removes them.