IntermediateWindow functions

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
SQL
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

SQL
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;
Result
dayrevenuerunning_totalavg_7_rowsavg_7_daysdays_in_window
2026-01-011001001001001
2026-01-021202201101102
2026-01-03803001001003
2026-01-04110410102.5102.54
2026-01-061505601121125
2026-01-0790650108.3108.36
2026-01-08130780111.4113.36
2026-01-09100880111.41106

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 JOIN makes missing days explicit as zero, which most dashboards want anyway.
  • RANGE with an interval is supported in PostgreSQL, Oracle, DuckDB and Spark SQL; SQL Server only supports RANGE with UNBOUNDED or CURRENT ROW, so fill the dates and use ROWS there.

Common mistakes

  • Writing only ORDER BY day with no frame. The default frame is RANGE 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 AVG ignores NULLs, so a NULL day lowers the divisor rather than counting as zero.

More on this topic in the SQL Interview Questions guide, or browse all SQL questions.