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.
- Put rows in order
- Choose the frame
- Add or compare
- Label the runs
- Check the final total
HOW THE PICTURE MAPS
| In the picture | In the work |
|---|---|
| The balance after each line | A running total |
| The lines the bank counts for each balance | The window frame |
| Comparing this month's statement with last month's | LAG |
| Paying in on days in a row | An island |
| A day with no payment | A 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.
| In the trick | What it stands for |
|---|---|
| So far | A running total grows row by row |
| Compared with the row before | LAG compares with the row before |
| Days in a row | Islands 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
- 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.
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.
- 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.
Publisher documentation opened on6November2026. Engine/version scope is explicit; other database implementations may differ. Teaching queries are synthetic, not a required publisher template.
- Market practice
SQLite: date/time functions ↗ — SQLite project · SQLite: date/time functions; ISO text/julianday/date modifiers, numeric subtraction contrast. Publisher page opened6November 2026.
Publisher documentation opened on6November2026. Engine/version scope is explicit; other database implementations may differ. Teaching queries are synthetic, not a required publisher template.
- Simplified educational illustration
Payments Signal editorial teaching models — Payments Signal
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.