Problems / Incremental Aggregation / Editorial
This problem contrasts two approaches for running totals:
```sql
SELECT a.month, a.region, SUM(b.revenue)
FROM monthly_financials a
JOIN monthly_financials b
ON a.region = b.region AND b.month <= a.month
GROUP BY a.month, a.region
```
This is O(n²) per partition — each row joins with all preceding rows.
SUM(revenue) OVER (PARTITION BY region ORDER BY month
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
This is O(n) per partition — the engine maintains a running accumulator.
Solve Incremental Aggregation yourself →