Stop 08 · Window FunctionsTopics · 6/9

Running Totals with SUM OVER

Part of the SQL Roadmap

In short

Finance teams love the question "how much have we made so far this month?". A running total answers it on every row, and SUM with OVER (ORDER BY ...) builds one in a single line.

SUM(amount) OVER (ORDER BY date) produces a cumulative total that grows row by row, often used for year-to-date figures.

The jaadu leans out of a car window on a winding mountain road, pointing proudly at a large mechanical odometer with rolling number wheels.

Think of it like this

The odometer on a road trip

On a Delhi to Manali drive, you cover 120 km on day one, 180 on day two and 90 on day three. The odometer doesn't show each day separately. It shows 120, then 300, then 390, always the total so far. A running total is your data's odometer.

Watch it happen

Daily revenue becomes a running total

1200
0
800
1
1500
2
600
3
900
4

1/5Five days of revenue in rupees, in date order.

The big ideas

What you need to know

ORDER BY makes it run

SUM(x) OVER () is a grand total. Add ORDER BY sale_date inside OVER and it becomes a cumulative total.

Reset with PARTITION BY

PARTITION BY DATE_TRUNC('month', sale_date) restarts the total each month, giving month-to-date figures.

Watch for duplicate dates

The default frame is RANGE, so rows with the same date are added together in one jump. Use ROWS for strict row-by-row totals.

Not just SUM

COUNT(*) OVER (ORDER BY ...) gives a running count and AVG gives a running average.

See it in code

Walk through the code

sql
1SELECT2  sale_date,3  revenue,4  SUM(revenue) OVER (5    ORDER BY sale_date6    ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW7  ) AS running_total,8  SUM(revenue) OVER (9    PARTITION BY DATE_TRUNC('month', sale_date)10    ORDER BY sale_date11  ) AS month_to_date12FROM daily_sales13ORDER BY sale_date;

1/3Order by date and add up every row from the start to the current one.

A running total for all time and one that resets every month. DATE_TRUNC is PostgreSQL. In MySQL use DATE_FORMAT(sale_date, '%Y-%m'), and in SQL Server use DATETRUNC (2022+) or YEAR and MONTH.

Save yourself the pain

Common mistakes

Oops

Forgetting ORDER BY inside OVER and getting the same big number on every row.

Do this instead

No ORDER BY means the window is the whole partition. Add ORDER BY sale_date to make it cumulative.

Oops

Running a total on raw orders where many share one date and seeing it jump.

Do this instead

Either sum to one row per day first, or use ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.

Your mission

Month-to-date dashboard

~25 min

Quick check

What does SUM(revenue) OVER () return on each row?

Go deeper

  1. 1Build a running total of daily revenue.
  2. 2Restart the running total each year with PARTITION BY year.
  3. 3Learn how duplicate dates affect the default frame and when to use ROWS instead.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge