← Writing

Why did our staffing reports say 52 and 18 when the plan was 26?

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.

Report A
52
SUM after a join
Report B
18
SUM(DISTINCT …)
Operations plan
26
8 + 8 + 10

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.

shifts
shift_idplanned_headcount
A8
B8
C10
shift_events
event_idshift_id
1A
2A
3B
4B
5C
6C

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 = 26

This 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 SUM that follows it. If you see a suspiciously round or large number, count the rows before the SUM.
  • 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.
staffing_puzzle.sqlRuns in SQLite 3 or PostgreSQL. No setup needed — all data is in the query.
Download SQL ↓

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 →