D8LooPFocus modeCODE WORKSPACE
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
| order_day | daily_revenue |
|---|---|
| 2026-01-12 | 200 |
| 2026-01-13 | 40 |
| 2026-01-15 | 220 |
| 2026-01-18 | 540 |
| 2026-01-20 | 60 |
Expected output
| order_day | daily_revenue | running_revenue |
|---|---|---|
| 2026-01-12 | 200 | 200 |
| 2026-01-13 | 40 | 240 |
| 2026-01-15 | 220 | 460 |
| 2026-01-18 | 540 | 1000 |
| 2026-01-20 | 60 | 1060 |
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.
Run the SQL query to inspect preview rows.