← Back to Case StudiesSQL work sample

Synthetic data. This is an independent exercise, not a production system.

How I evaluate a SQL answer

Overnight shifts, critical pull time responsibility, and as-of reporting — an independent work sample using synthetic shipment snapshots.

The Context

This example uses an illustrative warehouse-local shift schedule in America/Phoenix (UTC-7, no daylight saving). These hours are examples, not universal warehouse rules.

  • Day shift: 06:00–18:00 local time
  • Night shift: 18:00–06:00 the following day
  • Night shift operational date: the date on which the shift starts (the November 5 night shift runs from Nov 5 18:00 to Nov 6 06:00 local)

Critical pull time (CPT) is the scheduled deadline for orders to be ready to ship. Shipment responsibility follows CPT, not promise date. Promise date is a separate customer-delivery commitment.

Nov 5 night shift boundaries (UTC):

  • Shift start: Nov 5 18:00 Phoenix = 2026-11-06 01:00:00+00 UTC
  • Shift end / handover: Nov 6 06:00 Phoenix = 2026-11-06 13:00:00+00 UTC
  • Updated review: Nov 6 09:00 Phoenix = 2026-11-06 16:00:00+00 UTC

Shift boundaries use half-open intervals: cpt >= shift_start AND cpt < shift_end. Report cutoff is inclusive: available_at <= report_cutoff.

available_at represents when a snapshot became available to the reporting system. For this synthetic example, it is the same as ingestion time. In a real system, processing latency means availability follows ingestion.

Historical miss handling: If a shipment later becomes ready or ships, that does not erase its earlier missed deadline. A miss is recorded when status = 'missed_cpt' in a snapshot whose snapshot_time is at or after the shipment's CPT and whose available_at is at or after the shipment's CPT. A later ready or shipped status does not retroactively clear the miss. The query checks whether any eligible snapshot ever showed a miss, not whether the latest snapshot shows a miss.

The Data

Synthetic shipment snapshots from a snapshot-based pipeline. Each row represents the state of a shipment at the time the snapshot was captured. Multiple snapshots may exist for the same shipment.

For this historical-miss report, consider every qualifying snapshot available by the reporting cutoff. Later status changes do not erase an earlier missed deadline.

snapshot_time (UTC)shipment_idwarehousecpt (UTC)promise_datestatusavailable_at (UTC)
2026-11-06 02:00SHP-001WH-A2026-11-06 03:002026-11-06in_progress2026-11-06 02:01
2026-11-06 04:00SHP-001WH-A2026-11-06 03:002026-11-06missed_cpt2026-11-06 04:01
2026-11-06 12:00SHP-001WH-A2026-11-06 03:002026-11-06ready2026-11-06 12:01
2026-11-06 02:00SHP-002WH-A2026-11-06 09:002026-11-07in_progress2026-11-06 02:01
2026-11-06 10:00SHP-002WH-A2026-11-06 09:002026-11-07missed_cpt2026-11-06 10:01
2026-11-06 04:00SHP-003WH-A2026-11-06 13:002026-11-06in_progress2026-11-06 04:01
2026-11-06 13:01SHP-003WH-A2026-11-06 13:002026-11-06missed_cpt2026-11-06 13:02
2026-11-06 10:00SHP-004WH-A2026-11-06 05:002026-11-06missed_cpt2026-11-06 14:00
2026-11-06 10:00SHP-004WH-A2026-11-06 05:002026-11-06missed_cpt2026-11-06 14:00
2026-11-06 04:00SHP-005WH-B2026-11-06 04:002026-11-06missed_cpt2026-11-06 04:01
2026-11-06 04:00SHP-006WH-A2026-11-06 02:002026-11-08missed_cpt2026-11-06 04:01
2026-11-06 04:00SHP-007WH-A2026-11-05 22:002026-11-06missed_cpt2026-11-06 04:01
2026-11-06 04:00SHP-008WH-A2026-11-06 06:002026-11-06in_progress2026-11-06 04:01
2026-11-06 04:00SHP-009WH-A2026-11-06 01:002026-11-06missed_cpt2026-11-06 04:01

Fixture rules (Phoenix local → UTC conversion):

  • SHP-001: CPT Nov 5 20:00 Phoenix = 03:00 UTC Nov 6. Three snapshots: first shows in_progress (before CPT), second shows missed_cpt (after CPT, snapshot_time 04:00 >= CPT 03:00). Third snapshot shows ready at 12:00 UTC — this must NOT erase the historical miss. CPT within night shift [01:00, 13:00) UTC.
  • SHP-002: CPT Nov 6 02:00 Phoenix = 09:00 UTC Nov 6 (after midnight Phoenix but before shift ends at 06:00 Phoenix = 13:00 UTC). Two snapshots: first in_progress at 02:00 (before CPT), second missed_cpt at 10:00 (after CPT). CPT within night shift.
  • SHP-003: CPT exactly at Nov 6 06:00 Phoenix = 13:00 UTC Nov 6 (shift boundary). First snapshot is in_progress. Second snapshot shows missed_cpt at 13:01 UTC (after shift ends), available at 13:02 UTC. Both are excluded from night-shift responsibility by cpt < '2026-11-06 13:00:00+00'. The second snapshot also proves shift-end exclusion: even though it shows missed_cpt and is available by 16:00, its CPT belongs to the day shift. It is excluded from night-shift observation because the missed state was observed after the night shift ended (snapshot_time 13:01 >= shift_end 13:00).
  • SHP-004: CPT Nov 5 22:00 Phoenix = 05:00 UTC Nov 6. Snapshot captured at 10:00 UTC but available_at 14:00 UTC — after the 13:00 handover cutoff but before the 16:00 updated review. Duplicate delivery (two identical rows).
  • SHP-005: Different warehouse (WH-B). CPT Nov 5 21:00 Phoenix = 04:00 UTC Nov 6. Tests warehouse filtering.
  • SHP-006: CPT Nov 5 19:00 Phoenix = 02:00 UTC Nov 6. Promise date Nov 8 (distinct from CPT). Tests that promise date does not determine shift responsibility.
  • SHP-007: CPT Nov 5 15:00 Phoenix = 2026-11-05 22:00 UTC — in the preceding day shift (before the night shift starts at 01:00 UTC). Observed as missed during the night shift. Tests observation without night-shift responsibility.
  • SHP-008: CPT 06:00 UTC, status in_progress. Non-missed shipment — should not appear in either query.
  • SHP-009: CPT exactly at Nov 5 18:00 Phoenix = 01:00 UTC Nov 6 (shift start boundary). Included by cpt >= '2026-11-06 01:00:00+00'. Tests shift-start inclusion.

Question A: Responsibility

"As of the reporting cutoff, which shipments with CPT during the November 5 night shift were known to have missed CPT?"

Shipment responsibility follows CPT within the shift. We check whether any snapshot available by the report cutoff shows a missed CPT, where the snapshot was captured at or after the CPT and became available at or after the CPT. A later ready status does not erase the historical miss.

13:00 UTC handover cutoff

-- Responsibility: any snapshot showing a miss, available by the 13:00 UTC handover cutoff
-- Historical miss: snapshot_time >= cpt AND available_at >= cpt (miss cannot precede deadline)
-- A later ready status does not erase the miss
SELECT DISTINCT s.shipment_id, s.cpt
FROM shipment_snapshots s
WHERE s.status = 'missed_cpt'
  AND s.warehouse = 'WH-A'
  AND s.available_at <= '2026-11-06 13:00:00+00'
  AND s.snapshot_time >= s.cpt
  AND s.available_at >= s.cpt
  AND s.cpt >= '2026-11-06 01:00:00+00'
  AND s.cpt <  '2026-11-06 13:00:00+00'
ORDER BY s.cpt

Expected result: 4 shipments. SHP-001, SHP-002, SHP-006, SHP-009.

  • SHP-001: CPT 03:00 UTC within [01:00, 13:00). Snapshot at 04:00 (after CPT 03:00), available_at 04:01 (after CPT). Shows missed_cpt. The later ready snapshot at 12:00 does not erase this miss.
  • SHP-002: CPT 09:00 UTC within [01:00, 13:00). Missed snapshot at 10:00, available_at 10:01 — available by the 13:00 cutoff. Included.
  • SHP-003: CPT 13:00 UTC. Its in-shift snapshot is in_progress. Its missed snapshot was captured at 13:01 UTC, after the night shift ended, so it is excluded from the observation query. Its CPT is exactly at the shift-end boundary, so it is also excluded from night-shift responsibility.
  • SHP-004: CPT 05:00 UTC within the shift, but available_at 14:00 UTC — not available by the 13:00 cutoff. Excluded.
  • SHP-005: Excluded by warehouse = 'WH-A'.
  • SHP-006: CPT 02:00 UTC within [01:00, 13:00). Snapshot at 04:00 (after CPT 02:00), available_at 04:01 (after CPT). Promise date Nov 8 does not affect shift assignment.
  • SHP-007: CPT 2026-11-05 22:00 UTC — before the night shift starts at 01:00 UTC. Excluded.
  • SHP-008: status = 'in_progress'. Excluded.
  • SHP-009: CPT 01:00 UTC — exactly at shift start. Included by cpt >= '2026-11-06 01:00:00+00'. Snapshot at 04:00 (after CPT 01:00), available_at 04:01 (after CPT).

16:00 UTC updated review cutoff

-- Same query, different cutoff: 16:00 UTC
-- (only the available_at filter changes)
WHERE s.available_at <= '2026-11-06 16:00:00+00'

Expected result: 5 shipments. SHP-001, SHP-002, SHP-004, SHP-006, SHP-009.

At 16:00 UTC cutoff, SHP-004 now appears (available_at 14:00 <= 16:00, snapshot_time 10:00 >= cpt 05:00). SHP-002 was already available at the 13:00 cutoff. SHP-001's miss is preserved despite the later ready snapshot at 12:00 UTC.

SHP-004 demonstrates genuine late arrival: the snapshot was captured during the shift (10:00 UTC) but became available to the reporting system after handover (14:00 UTC). The updated report at 16:00 includes information that was not available at the 13:00 handover.

Question B: Observation during shift

"As of the reporting cutoff, which shipments were observed as late in snapshots captured during the November 5 night shift?"

This asks about observation, not shipment responsibility. Because the fixture contains only snapshots — not individual event timestamps — we use snapshot_time (when the source captured the operational state) to determine what was observed during the shift. A snapshot taken during a shift can contain events or shipment states originating earlier. Snapshot time establishes when a state was observed, not necessarily when an event occurred.

-- Observation: shipments observed as late in snapshots captured during the night shift
-- Use the 16:00 UTC cutoff so all snapshots are available
SELECT DISTINCT s.shipment_id, s.cpt
FROM shipment_snapshots s
WHERE s.status = 'missed_cpt'
  AND s.warehouse = 'WH-A'
  AND s.available_at <= '2026-11-06 16:00:00+00'
  AND s.snapshot_time >= '2026-11-06 01:00:00+00'
  AND s.snapshot_time <  '2026-11-06 13:00:00+00'
ORDER BY s.cpt

Expected result: 6 shipments. SHP-001, SHP-002, SHP-004, SHP-006, SHP-007, SHP-009.

Why this differs from Question A at 16:00:

  • SHP-007: CPT in the preceding day shift (2026-11-05 22:00 UTC). Observed as missed during the night shift (snapshot at 04:00 UTC). Appears in observation, not responsibility.
  • SHP-009: CPT exactly at shift start (01:00 UTC). Appears in both responsibility and observation.
  • SHP-003: Its in-shift snapshot is in_progress. Its missed snapshot was captured at 13:01 UTC, after the night shift ended, so it is excluded from the observation query. Its CPT is exactly at the shift-end boundary, so it is also excluded from night-shift responsibility.

The fundamental difference: responsibility follows CPT within the shift; observation follows the timestamp of the snapshot that recorded the state.

Why This Matters

Two stakeholders asking about "the night shift" need different answers:

  • The shift lead at handover needs to know which shipments were their responsibility — those whose CPT fell within the shift. They own the outcome for those shipments.
  • The operations analyst needs to know which late states were observed during the shift window. This includes shipments responsible to another shift.

A query that uses COUNT(DISTINCT shipment_id) without selecting the correct snapshot state will give the wrong answer. A query that filters on promise_date instead of CPT will assign shipments to the wrong shift.

A query that keeps only the latest status per shipment will miss historical misses — if a shipment later becomes ready, the earlier miss is erased. The correct approach checks whether any eligible snapshot ever showed a miss. Historical misses use any qualifying evidence available by the cutoff; current-state reporting selects the latest eligible snapshot. These are two different questions.

This is the kind of judgment that matters in production: knowing which timestamp defines responsibility, how to select the correct snapshot state, when two seemingly similar questions produce different results, and how to preserve historical evidence of a missed deadline.

Reproducible SQL

You can run these queries yourself. The table definition, fixture data, and both queries use PostgreSQL-compatible syntax.

-- Table definition
CREATE TABLE shipment_snapshots (
  snapshot_time  TIMESTAMP WITH TIME ZONE NOT NULL,
  shipment_id    TEXT NOT NULL,
  warehouse      TEXT NOT NULL,
  cpt            TIMESTAMP WITH TIME ZONE NOT NULL,
  promise_date   DATE,
  status         TEXT NOT NULL,
  available_at   TIMESTAMP WITH TIME ZONE NOT NULL
);

-- Fixture data (14 rows)
INSERT INTO shipment_snapshots VALUES
  -- SHP-001: CPT 03:00 UTC, three snapshots (in_progress → missed_cpt → ready)
  ('2026-11-06 02:00:00+00', 'SHP-001', 'WH-A', '2026-11-06 03:00:00+00', '2026-11-06', 'in_progress',  '2026-11-06 02:01:00+00'),
  ('2026-11-06 04:00:00+00', 'SHP-001', 'WH-A', '2026-11-06 03:00:00+00', '2026-11-06', 'missed_cpt', '2026-11-06 04:01:00+00'),
  ('2026-11-06 12:00:00+00', 'SHP-001', 'WH-A', '2026-11-06 03:00:00+00', '2026-11-06', 'ready',      '2026-11-06 12:01:00+00'),
  -- SHP-002: CPT 09:00 UTC, two snapshots (in_progress → missed_cpt, miss AFTER CPT)
  ('2026-11-06 02:00:00+00', 'SHP-002', 'WH-A', '2026-11-06 09:00:00+00', '2026-11-07', 'in_progress',  '2026-11-06 02:01:00+00'),
  ('2026-11-06 10:00:00+00', 'SHP-002', 'WH-A', '2026-11-06 09:00:00+00', '2026-11-07', 'missed_cpt', '2026-11-06 10:01:00+00'),
  -- SHP-003: CPT exactly at shift boundary 13:00 UTC, two snapshots
  ('2026-11-06 04:00:00+00', 'SHP-003', 'WH-A', '2026-11-06 13:00:00+00', '2026-11-06', 'in_progress',  '2026-11-06 04:01:00+00'),
  ('2026-11-06 13:01:00+00', 'SHP-003', 'WH-A', '2026-11-06 13:00:00+00', '2026-11-06', 'missed_cpt', '2026-11-06 13:02:00+00'),
  -- SHP-004: CPT 05:00 UTC, late snapshot (two identical rows = duplicate delivery)
  ('2026-11-06 10:00:00+00', 'SHP-004', 'WH-A', '2026-11-06 05:00:00+00', '2026-11-06', 'missed_cpt', '2026-11-06 14:00:00+00'),
  ('2026-11-06 10:00:00+00', 'SHP-004', 'WH-A', '2026-11-06 05:00:00+00', '2026-11-06', 'missed_cpt', '2026-11-06 14:00:00+00'),
  -- SHP-005: Different warehouse WH-B
  ('2026-11-06 04:00:00+00', 'SHP-005', 'WH-B', '2026-11-06 04:00:00+00', '2026-11-06', 'missed_cpt', '2026-11-06 04:01:00+00'),
  -- SHP-006: CPT 02:00 UTC, promise date Nov 8
  ('2026-11-06 04:00:00+00', 'SHP-006', 'WH-A', '2026-11-06 02:00:00+00', '2026-11-08', 'missed_cpt', '2026-11-06 04:01:00+00'),
  -- SHP-007: CPT in preceding day shift, observed as missed during night shift
  ('2026-11-06 04:00:00+00', 'SHP-007', 'WH-A', '2026-11-05 22:00:00+00', '2026-11-06', 'missed_cpt', '2026-11-06 04:01:00+00'),
  -- SHP-008: Non-missed, in_progress
  ('2026-11-06 04:00:00+00', 'SHP-008', 'WH-A', '2026-11-06 06:00:00+00', '2026-11-06', 'in_progress',  '2026-11-06 04:01:00+00'),
  -- SHP-009: CPT exactly at shift start 01:00 UTC
  ('2026-11-06 04:00:00+00', 'SHP-009', 'WH-A', '2026-11-06 01:00:00+00', '2026-11-06', 'missed_cpt', '2026-11-06 04:01:00+00');

Expected Results

Results verified using PGlite (PostgreSQL-compatible engine) against the published 14-row fixture. All counts and shipment IDs match.

The schema, fixture, queries, and test are on GitHub: timothypike/sql-shift-responsibility (opens in a new tab). Run npm ci && npm test to check the 4, 5, and 6 results and each edge case.

QueryCutoffCountShipment IDsKey distinction
Responsibility (CPT in shift)13:00 UTC4SHP-001, SHP-002, SHP-006, SHP-009CPT within [01:00, 13:00) UTC, miss snapshot available by cutoff, snapshot_time >= CPT
Responsibility (CPT in shift)16:00 UTC5SHP-001, SHP-002, SHP-004, SHP-006, SHP-009SHP-004 newly available; SHP-002 was already included at handover. SHP-001 miss preserved despite later ready status
Observation (snapshot in shift)16:00 UTC6SHP-001, SHP-002, SHP-004, SHP-006, SHP-007, SHP-009Includes SHP-007 (CPT in prior shift) and SHP-009 (CPT at shift start). Excludes SHP-003: its in-shift snapshot is in_progress, and its missed snapshot was captured after the shift ended.

Boundary tests:

  • Shift-end exclusion: SHP-003's CPT is exactly at 13:00 UTC. The half-open interval cpt < '2026-11-06 13:00:00+00' excludes it from responsibility. Its second snapshot shows missed_cpt at 13:01 UTC (after shift ends), available at 13:02 UTC — excluded from both responsibility (CPT not in shift) and observation (snapshot_time after shift end).
  • Shift-start inclusion: SHP-009's CPT is exactly at 01:00 UTC. The interval cpt >= '2026-11-06 01:00:00+00' includes it in responsibility.
  • After-cutoff test: SHP-004's snapshot is captured at 10:00 UTC (within the shift) but available_at 14:00 UTC (after the 13:00 handover). It does not appear in the 13:00 responsibility query but does appear in the 16:00 query.
  • Duplicate delivery: SHP-004 has two identical rows. DISTINCT ensures it is counted once.
  • Historical miss preservation: SHP-001 has a later ready snapshot at 12:00 UTC. The query checks for any snapshot showing missed_cpt (with snapshot_time >= CPT), so the miss is preserved.
  • Warehouse filtering: SHP-005 is in WH-B and is excluded by warehouse = 'WH-A'.

Handover reporting cutoff: The 13:00 UTC handover cutoff is the policy chosen for this example. Not every daily business review excludes late-arriving records. The 16:00 UTC updated review shows what happens when more data becomes available after the initial handover.