Overview
Show each store's revenue adding up day by day with SUM() OVER, partitioned by store and ordered by day.
- SUM() OVER (ORDER BY ...)
- PARTITION BY restarts the total
- Without ORDER BY you get the grand total
Short Course
SUM(x) OVER (ORDER BY day) adds up row by row: a running total. Add
PARTITION BY store to restart it for each store. Trap: without ORDER BY
inside OVER, every row gets the grand total instead.
SELECT day, signups,
SUM(signups) OVER (ORDER BY day) AS total_so_far
FROM daily_signups;
Task
Return each store, day, the day's revenue and the store's running total
so far, ordered by store, then day. In the database below, the north store
has made 140 after two days.
Database
Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.
| storeTEXT | dayTEXT | revenueINTEGER |
|---|---|---|
| north | 2024-03-01 | 100 |
| south | 2024-03-01 | 50 |
| north | 2024-03-02 | 40 |
| south | 2024-03-02 | 10 |
| north | 2024-03-03 | 60 |
Test Suites
Pending
Next
Output