Running Total Revenue per Customer
Preview mode. Log in to edit, run, submit, and save progress.
Running Total Revenue per Customer
You are given a table of customer purchases. Each purchase has a customer ID, an amount, and a date. Write a SQL query that returns every purchase row enriched with a running_total column: the cumulative sum of amount for that customer up to and including the current row, ordered by purchase_date. Return all columns (id, customer_id, amount, purchase_date, running_total), ordered by customer_id then purchase_date. Table: Purchases
| Column Name | Type | Description |
|---|---|---|
| id | INT | Primary key |
| customer_id | INT | ID of the customer |
| amount | INT | Purchase amount |
| purchase_date | DATE | Date of the purchase |
Examples
Use SUM() as a window function partitioned by customer_id and ordered by purchase_date with ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.
Approach hint
Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.
Common mistake
Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.