How do you compute a running total and a 7-day moving average?
Scenario
A finance dashboard shows daily revenue with a running total and a 7-day moving average. The shop was closed on 5 January, so that date has no row.
Interview answerTry answering first, then reveal
Running total: SUM(revenue) OVER (ORDER BY day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW). Moving average: AVG(revenue) OVER (ORDER BY day ROWS BETWEEN 6 PRECEDING AND CURRENT ROW), which is the last 7 rows. If dates can be missing, use a RANGE frame with an interval, or fill the missing dates first, so the window really covers 7 calendar days.
Explanation
The frame clause says which rows around the current row the function sees. ROWS counts physical rows; RANGE uses the value of the ORDER BY column, so RANGE BETWEEN INTERVAL 6 DAYS PRECEDING AND CURRENT ROW means "dates from 6 days ago to today", however many rows that is.
When a date is missing, a 7-row window reaches back further than 7 days. In the output below, from 8 January the two averages differ because the 7-row window still includes 1 January.
The first rows have fewer than 7 days behind them, so their "7-day" average is really a 1- to 6-day average. Show it, hide it, or mark it, but decide deliberately.
Worked example
Run on DuckDB when this page was built; the results shown are the real output.
Sample data
CREATE TABLE daily_sales (day DATE, revenue INT);
INSERT INTO daily_sales VALUES
('2026-01-01', 100), ('2026-01-02', 120), ('2026-01-03', 80), ('2026-01-04', 110),
('2026-01-06', 150), ('2026-01-07', 90), ('2026-01-08', 130), ('2026-01-09', 100);Running total, 7-row and 7-day averages
SELECT day, revenue,
SUM(revenue) OVER (ORDER BY day
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total,
ROUND(AVG(revenue) OVER (ORDER BY day
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW), 1) AS avg_7_rows,
ROUND(AVG(revenue) OVER (ORDER BY day
RANGE BETWEEN INTERVAL 6 DAYS PRECEDING AND CURRENT ROW), 1) AS avg_7_days,
COUNT(*) OVER (ORDER BY day
RANGE BETWEEN INTERVAL 6 DAYS PRECEDING AND CURRENT ROW) AS days_in_window
FROM daily_sales
ORDER BY day;| day | revenue | running_total | avg_7_rows | avg_7_days | days_in_window |
|---|---|---|---|---|---|
| 2026-01-01 | 100 | 100 | 100 | 100 | 1 |
| 2026-01-02 | 120 | 220 | 110 | 110 | 2 |
| 2026-01-03 | 80 | 300 | 100 | 100 | 3 |
| 2026-01-04 | 110 | 410 | 102.5 | 102.5 | 4 |
| 2026-01-06 | 150 | 560 | 112 | 112 | 5 |
| 2026-01-07 | 90 | 650 | 108.3 | 108.3 | 6 |
| 2026-01-08 | 130 | 780 | 111.4 | 113.3 | 6 |
| 2026-01-09 | 100 | 880 | 111.4 | 110 | 6 |
In production
- Aggregate to one row per day before applying the window. Windowing millions of raw orders is slower and gives several rows per date.
- For month-to-date totals add
PARTITION BY date_trunc('month', day)so the total restarts each month. - A calendar (date spine) table joined with
LEFT JOINmakes missing days explicit as zero, which most dashboards want anyway. RANGEwith an interval is supported in PostgreSQL, Oracle, DuckDB and Spark SQL; SQL Server only supportsRANGEwithUNBOUNDEDorCURRENT ROW, so fill the dates and useROWSthere.
Common mistakes
- Writing only
ORDER BY daywith no frame. The default frame isRANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, which gives tied dates the same running total. - Calling a 7-row window "7 days" when dates have gaps.
- Forgetting that
AVGignores NULLs, so a NULL day lowers the divisor rather than counting as zero.
Related questions
More on this topic in the SQL Interview Questions guide, or browse all SQL questions.