TOPIC 26/3016 MINNOT STARTED

SQL for BAs: running totals, month-on-month change and streaks

Write a running total or a moving average, compare one month with the last using LAG, find days in a row, and say why the frame of a window should be spelled out.

L1–L4 are part of the Pro pass

L0 Explain simply

Analogy: a bank statement. Each line shows the payment. The balance after that line is a . Three pay-ins on three days in a row are a streak. A day with no line is a gap, not a zero you forgot.

Daily List is a made-up to-do app (SYNTHETIC / TRAINING ONLY). Nina asks how many tasks have been added so far. She also asks whether this month beat last month, and who finished a task on days in a row.

What it is: (Structured Query Language) can read rows in order. A table is a grid, a row is one record, and a column is one fact. A running total is a sum that grows from the first row up to the current row. A is the set of rows the function may see, such as "everything so far" or "this row and the two before it". Gaps and islands is the question that looks for unbroken runs, called islands, and the breaks between them, called gaps. A plain SUM without OVER is the neighbour. It returns one number for the whole pile, not a balance on each line.

Why it matters: Two tasks added on the same day can share one balance or show two. If Sam does not name the frame, the next reader cannot tell which question was asked. A streak count that forgets a repeated day also looks tidy and is wrong.

A frame counted in rows is not a frame counted in days. A quiet day has no row.

PLAIN WORDS · 1 in this section
unbroken
whole, with no gap

PICTURE IT

The picture is the one the lesson above opens with. The table below shows how each part of it matches the work.

THE REAL JOB The picture helps a newcomer remember that a adds line by line, a frame says which lines count, and a streak is a run with no break.

  1. Put rows in order
  2. Choose the frame
  3. Add or compare
  4. Label the runs
  5. Check the final total
Reading rows in order

HOW THE PICTURE MAPS

How each part of the picture matches the work
In the pictureIn the work
The balance after each lineA running total
The lines the bank counts for each balanceThe window frame
Comparing this month's statement with last month'sLAG
Paying in on days in a rowAn island
A day with no paymentA gap

WHERE THIS PICTURE STOPS

  • A statement is printed once, but a query can be run again on new rows.
  • A bank counts days on the calendar, while a frame in rows counts only rows that exist.
  • Two payments on one day appear as two lines. A query may treat tied rows as one step.

HOOK

Add as you go, say which rows count.

REMEMBER IT THIS WAY

A rule of thumb: if you see this, think that.

Usually, "so far" means a , "compared with the row before" means LAG, and "days in a row" means islands and gaps.

What each part of the trick stands for
In the trickWhat it stands for
So farA running total grows row by row
Compared with the row beforeLAG compares with the row before
Days in a rowIslands are unbroken runs, and gaps are the breaks

One question can need two at once, such as a running total inside each streak.

TELL A FRIEND

A adds as it goes, and the frame says which rows count. LAG compares a row with the one before, and a date minus a row number labels each run of days.

OFTEN MIXED UP WITH SQL for BAs: window functions and ranking. That topic ranks rows inside groups. This one adds up, compares with the row before and finds runs.

PRACTISE AND KEEP

Sources for this topic — 4 sources
  1. Market practice

    Window Functions (PostgreSQL function reference) ↗ — PostgreSQL Global Development Group · PostgreSQL 18, §9.22 rank, ROW_NUMBER returns bigint, LAG/LEAD; publisher page opened6November 2026. Dialect scope explicit.

    §9.22 rank, ROW_NUMBER bigint, LAG/LEAD. PostgreSQL18 implementation, not a universal SQL mandate. · Checked 2026-11-06

    PostgreSQL18 publisher documentation opened on6November2026: §9.22 rank, ROW_NUMBER bigint, LAG/LEAD. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.

  2. Market practice

    PostgreSQL18: date/time functions ↗ — PostgreSQL Global Development Group · PostgreSQL 18: date/time functions; §9.9 typed date arithmetic and truncation/timezones. Publisher page opened6November 2026.

    §9.9 typed date arithmetic and truncation/timezones. · Checked 2026-11-06

    Publisher documentation opened on6November2026. Engine/version scope is explicit; other database implementations may differ. Teaching queries are synthetic, not a required publisher template.

  3. Market practice

    SQLite: date/time functions ↗ — SQLite project · SQLite: date/time functions; ISO text/julianday/date modifiers, numeric subtraction contrast. Publisher page opened6November 2026.

    ISO text/julianday/date modifiers, numeric subtraction contrast. · Checked 2026-11-06

    Publisher documentation opened on6November2026. Engine/version scope is explicit; other database implementations may differ. Teaching queries are synthetic, not a required publisher template.

  4. Simplified educational illustration

    Payments Signal editorial teaching models — Payments Signal

    This site's own simplified teaching models. · Checked 2026-07-12

    What this simplifies: Daily List people, data and exercises are invented for teaching. Query results belong to the declared fixture and database version. Authorization, business identity, time bounds and unknown-value policy must be agreed in context; no universal mandatory query template is implied.

    Used wherever diagrams, scenarios, figures, or example values are didactic constructions rather than sourced facts; every such use carries a simplifications disclosure. All people, companies, banks, and list entries in examples are fictional.

Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.