Field note · August 20, 2025

A running total that does not lie about ties

running total against a real schema, and the clause in it that is doing the work.

1 min read ·Analytics engineering ·sql

A running total that does not lie about ties comes up often enough to be worth a note. grid-energy-load is a convenient thing to try it on — 8,760 rows, one row per hour.

sql
select hour_at,
       load_mw,
       sum(load_mw) over (
         order by hour_at
         rows between unbounded preceding and current row
       ) as running_load_mw
from grid_energy_load
order by hour_at;

The frame clause is not optional decoration. Leave it out and you get range between unbounded preceding and current row, which includes every peer row with the same ordering value — so on a column with duplicate timestamps the running total jumps early and no error is raised.

Run it against the real file in the SQL playground — the engine there is enough of a SQL implementation to execute this as written, and the dataset is already loaded. The pattern page has the version with the failure modes spelled out.

Window functions are the difference between SQL that describes rows and SQL that describes sequences. Almost every "we exported it to pandas to do this bit" turns out to be one of these five.

AE

Analytics engineering

Turning warehouses full of raw tables into models an analyst can trust without asking anyone.

Dimensional modelling, grain, slowly changing dimensions, metric definitions, and the SQL that survives production. Most "we need a data scientist" problems are "we need one correct table" problems.

Related

All field notes    2025 archive