Running Totals &
Moving Averages
Master SQL window functions to compute cumulative sums and sliding averages: essential tools for time-series analysis and business reporting.
What Is a Running Total?
A running total (also called a cumulative sum) is the sum of all values from the first row up to and including the current row, when rows are ordered by some criterion like date.
What Is a Moving Average?
A moving average (also called a rolling average) computes the mean of a fixed-size window of rows surrounding the current row. It smooths out short-term fluctuations to reveal trends.
Why Running Totals & Averages Matter
The Window Function Pattern
Every running total and moving average in SQL follows the same structure: an aggregate function paired with an OVER clause that defines the window.
Running Total: The Frame Clause
For a running total, you want every row from the start of the partition up to the current row:
Moving Average: The Sliding Frame
For a 3-row moving average, you want a fixed-width window that slides along with the current row:
Running Total vs Moving Average
| Running Total | Moving Average | |
|---|---|---|
| Aggregate | SUM() | AVG() |
| Frame start | UNBOUNDED PRECEDING | N PRECEDING |
| Frame end | CURRENT ROW | CURRENT ROW |
| Window grows? | Yes: expands each row | No: fixed width, slides |
| Result trend | Always non-decreasing (for positive values) | Smoothed version of data |
| Use case | YTD revenue, account balance | 7-day trend, seasonal smoothing |
How to Write the Query
Example 1: Running Total of Sales
Given a table daily_sales with columns sale_date and amount, compute the cumulative revenue:
Example 2: 3-Day Moving Average
Smooth out daily fluctuations by averaging the current day and the two preceding days:
Example 3: Partitioned Running Total
If you have multiple stores, you probably want a separate running total for each one:
ROWS vs RANGE: A Key Distinction
The Default Frame Trap
What happens if you write a window function without specifying ROWS BETWEEN?
Common Mistakes to Avoid
Quick Reference Cheat Sheet
| Goal | SQL Pattern |
|---|---|
| Running Total | SUM(col) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) |
| 3-Row Moving Avg | AVG(col) OVER (ORDER BY date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) |
| 7-Row Moving Avg | AVG(col) OVER (ORDER BY date ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) |
| Per-Group Running Total | SUM(col) OVER (PARTITION BY group ORDER BY date ROWS ...) |
| Running Count | COUNT(*) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) |
Question 1 of 5
Which frame clause produces a running total?
Question 2 of 5
To compute a 5-day moving average, how many rows should you specify as PRECEDING?
Question 3 of 5
What does PARTITION BY do in a window function?
Question 4 of 5
Consider this query:
SELECT sale_date, amount, AVG(amount) OVER (ORDER BY sale_date) AS avg_col FROM daily_sales;
The table has rows where multiple sales occur on the same date.
What potential problem does this query have?
Question 5 of 5
Given daily sales of 10, 20, 30, 40 on four consecutive days, what is the running total on Day 3?