Conditional totals and pivot-style reports in a query
Group many rows into one line each, with a column per status. Learn conditional totals with CASE (a rule-based choice) and a row filter, and when a pivot report fits.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: sorting coins while you count. A clerk can count a pile in one go. A better clerk makes a stack for each kind of coin and reports every stack on one line.
Daily List is a made-up to-do app (SYNTHETIC / TRAINING ONLY). Sam, the , wants one line per list, with a count of done tasks and a count of open tasks.
What it is: (Structured Query Language) can total a table. A table is a grid, a row is one record, and a column is one fact. A counts or adds only the rows that pass a test. A filtered aggregate is that idea written as FILTER on one total, so other totals in the same query still see every row. A pivot-style report turns those totals into columns, with one row per group. WHERE would drop rows for every column. A conditional total drops them for one column only. That is the neighbour.
Why it matters: Nina asks for done, open and the total on one line. Three separate queries can drift apart. One query keeps them in step, but only if the columns add up.
A new status needs a new column. The query does not invent a column for a status it has never seen.
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 Sorting coins into stacks while counting them.
- Group
- Add
- Check
HOW THE PICTURE MAPS
| In the picture | In the work |
|---|---|
| The pile of coins | The table rows |
| A stack for one kind | A conditional total |
| The report of all stacks | A pivot-style report |
| A stack with its own rule | A filtered aggregate |
WHERE THIS PICTURE STOPS
- A pile is read by hand, but a query reads rows by rule.
- A clerk adds a stack by looking, but a query needs a new column.
- Coins are all different sizes, but rows have the same shape.
HOOK
One pass, many stacks.
TELL A FRIEND
Coins: one pile, one stack for each kind.
OFTEN MIXED UP WITH SQL for BAs: GROUP BY and counts. That topic groups and counts rows. This one adds a column for each value in the same query.
PRACTISE AND KEEP
Sources for this topic — 4 sources
- Market practice
Aggregate functions (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL 18, §9.21 aggregates, NULL and empty input; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: §9.21 aggregates, NULL and empty input. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
Value expressions and aggregate filters (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL 18, §4.2.7–8 aggregate/window expressions; §4.2.14 evaluation limits; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: §4.2.7–8 aggregate/window expressions; §4.2.14 evaluation limits. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
Table expressions and joined tables (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL 18, §7.2 joined tables, grouping and grouping sets; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: §7.2 joined tables, grouping and grouping sets. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- 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.