SQL · Level 3 Window functions

SQL Running Totals

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

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;

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.

Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.

sales 5 rows
storeTEXTdayTEXTrevenueINTEGER
north2024-03-01100
south2024-03-0150
north2024-03-0240
south2024-03-0210
north2024-03-0360
solution.sql
Test Suites Pending
Output