CODE WORKSPACE

Running daily revenue

Return order_day, daily_revenue, and running_revenue (cumulative revenue through that day). Order by order_day.

daily CTE with date(created_at) and SUM(total_amount), then SUM(daily_revenue) OVER (ORDER BY order_day ...).

Sample input

daily (from orders)Daily revenue from GROUP BY date(created_at). 2026-01-12 is 80 + 120 = 200.
order_daydaily_revenue
2026-01-12200
2026-01-1340
2026-01-15220
2026-01-18540
2026-01-2060

Expected output

Expected outputFirst 5 of 11 days. running_revenue accumulates the daily values; it reaches 2160 on the last day.
order_daydaily_revenuerunning_revenue
2026-01-12200200
2026-01-1340240
2026-01-15220460
2026-01-185401000
2026-01-20601060

Constraints

Aggregate daily revenue in a CTE (GROUP BY date(created_at)), then SUM(daily_revenue) OVER (ORDER BY order_day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW). Order by order_day.

Expected skills

Two-stage aggregation: GROUP BY to a daily grain, then an ordered running-total window.

SQL
Loading...

AI evaluation

Run the SQL query to inspect preview rows.