The morning shift runs at three sites. Each site files two events — a launch event and a close event — and each event record carries the planned headcount for that shift. Someone asks for total planned headcount across the day. Two reports come back from the same database: one says 52, one says 18. The operations plan said 26.
One of them is right. The other two are wrong in different directions. Try to pick one before reading on.
The data
Three shifts. Two events each. The shifts table holds one row per shift with its planned headcount. The shift_events table holds one row per event with the shift it belongs to.
| shift_id | planned_headcount |
|---|---|
| A | 8 |
| B | 8 |
| C | 10 |
| event_id | shift_id |
|---|---|
| 1 | A |
| 2 | A |
| 3 | B |
| 4 | B |
| 5 | C |
| 6 | C |
Why Report A says 52
Report A joins the two tables and sums headcount:
SELECT SUM(s.planned_headcount)
FROM shift_events e
JOIN shifts s ON s.shift_id = e.shift_id;The join produces six rows — one for each event. Shift A's headcount of 8 appears on rows 1 and 2. Shift B's 8 appears on rows 3 and 4. Shift C's 10 appears on rows 5 and 6. The SUM adds all six: 8 + 8 + 8 + 8 + 10 + 10 = 52. Each shift's headcount was counted once per event, not once per shift.
Why Report B says 18
Report B tries to fix it with DISTINCT:
SELECT SUM(DISTINCT s.planned_headcount)
FROM shift_events e
JOIN shifts s ON s.shift_id = e.shift_id;SUM(DISTINCT x) deduplicates values, not rows. Shift A and Shift B both have headcount 8. DISTINCT collapses those into one 8. The sum becomes 8 + 10 = 18. It dropped a whole shift.
The correct query
Aggregate at the shift level before summing. The goal is total planned headcount across shifts that appear in the event feed — one row per shift, not one row per event.
WITH represented_shifts AS (
SELECT DISTINCT shift_id
FROM shift_events
)
SELECT SUM(s.planned_headcount) AS total_planned_headcount
FROM shifts s
JOIN represented_shifts r ON r.shift_id = s.shift_id;
-- Result: 8 + 8 + 10 = 26This sums one row per shift. It includes only shifts that appear in shift_events — the reporting population. A shift with no events contributes zero. A shift staffed at the same headcount as another shift still counts separately because the join is on shift_id, not on the headcount value.
What to watch for
- A join that multiplies rows before aggregation inflates any
SUMthat follows it. If you see a suspiciously round or large number, count the rows before theSUM. SUM(DISTINCT x)deduplicates the numeric value, not the entity. It's rarely the right fix for a fan-out join.COUNT(DISTINCT id)works because IDs are unique; headcount values are not.- Deciding which shifts belong in the reporting population is a business rule, not a SQL rule. Document it. "Shifts with at least one event" is the definition used here.
The verified SQL file runs the three queries in one statement and returns all three results so you can compare them directly. It runs in SQLite 3 (no install needed on macOS) or PostgreSQL.
The underlying data model is covered in more depth in the SQL Data Warehouse architecture example, which shows how this kind of join problem comes up in multi-source reporting and what the corrected aggregate looks like in a larger context.
Working through a disagreement between two reports?
Talk through the problem →