SQL Window Functions: Running Totals, Moving Averages, and Cumulative Metrics/Running total per category
Reset the running total by partition.
Section: Moving windows
Running total per category
sql
sql
SELECT month,
region,
sales,
SUM(sales) OVER (
PARTITION BY region
ORDER BY month
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) AS regional_running_total
FROM monthly_sales;Explanation
Combining `PARTITION BY` and `ORDER BY` is the backbone of grouped time-series analysis.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Moving windows