Subscribe free
Alloconomy 21 Nov 2026 9 min read

One order line in two parcels

Your first query reports twelve ordered units for a six-unit line.

The interview prompt

Your first query reports twelve ordered units for a six-unit line. A colleague suggests SUM(DISTINCT ordered_qty). Use a second order to show why this fix fails, then build the daily report.

Build a date spine and preserve quiet days

Use line A with six units accepted Friday, split into Saturday and Monday dispatches of four and two. Add line B, another six units accepted Friday, still awaiting shipment. The correct Friday ordered total is twelve. A distinct sum of six-valued rows yields six. The numeric value cannot identify the business event.

The report needs one row per business day, including Sunday. Aggregate each process to that grain before joining it to the calendar. Here the day columns are already derived in the agreed business timezone. Each source has been checked for completeness, so an absent daily aggregate can legitimately mean zero.

WITH o AS (
  SELECT order_day AS day, SUM(qty) AS ordered
  FROM accepted_line GROUP BY order_day
), s AS (
  SELECT ship_day AS day, SUM(qty) AS shipped
  FROM dispatch_line GROUP BY ship_day
)
SELECT c.day, COALESCE(o.ordered, 0) AS ordered,
       COALESCE(s.shipped, 0) AS shipped
FROM reporting_days AS c
LEFT JOIN o ON c.day = o.day
LEFT JOIN s ON c.day = s.day
ORDER BY c.day;

Friday through Monday should return (12, 0), (0, 4), (0, 0), and (0, 2). If the buyer instead asks how many Friday orders have shipped by Monday, keep the order-line relationship and select the cohort by acceptance date. Daily totals have discarded that relationship.

The model behind the answer

Suppose the candidate joins the Friday order line to its two dispatch rows. Each result row now carries the original ordered quantity of six. A dashboard sums the column and reports twelve ordered. Every individual row looks plausible.

I’d draw two small facts. One holds the accepted order line with six ordered. The other holds the two dispatch lines with four and two shipped. Keep the order-line reference in both so support can trace the school’s order. To compare daily totals, aggregate each fact using its own event date and bring the results together on the reporting calendar. Friday remains visible even though nothing shipped that day.

Ask what changes if the buyer cancels the final two copies on Sunday instead. There would be six ordered, four shipped, two canceled, and zero still owed. That example exposes a missing event in a model that only subtracts shipments from orders.

Try it in SQLite

When six books become twelve

Run the join for line A. Compare the two totals, then inspect the individual joined rows. Line B is another real order for six.

Tables and sample data

Each run starts with fresh sample data. Use Ctrl+Enter or ⌘+Enter to run. Only your browser processes the query.

Turn a failed review into a regression test

A proposed fix changes the aggregation to SUM(DISTINCT qty). In a pull request with only order A, that fix looks good. Before asking an agent to revise it, add order B with the same quantity and no dispatches. Write Friday’s expected order total on paper.

Reviewer decision. Friday must show twelve ordered. Saturday and Monday contribute four and two shipped on their own dates. Review both metrics separately. Keep B in the fixture when the implementation changes again. An absent dispatch file still needs its own completeness status.

Further reading. The Data Warehouse Toolkit, 3rd ed., on separate aggregation of facts before aligning results.

One more question

The order system is complete but Saturday’s dispatch feed has missed its deadline. Is Saturday’s shipped value still zero if the row is absent?

Check your reasoning

Show it as incomplete or suppress the certified value. A calendar provides a reporting population. A source-completeness control supplies the evidence needed to interpret missing activity as zero.

Previous · Draw the first star · Next · Transaction facts remember occurrences · Complete series map