Compare shipments and refunds without multiplying either
An order has two dispatches worth $80 and $40 and three refunds of $5, $7, and $8.
The interview prompt
An order has two dispatches worth $80 and $40 and three refunds of $5, $7, and $8. Predict the row count and both sums after joining on order ID. Then write a query that also preserves an order appearing only in refunds.
Repair the join one population at a time
Two dispatches times three refunds gives six pairs. Each dispatch is repeated three times and each refund twice. The resulting $360 and $40 are mathematically predictable, which makes this a quick way to test your mental model at the whiteboard. Aggregating after this join cannot identify the original contributions.
WITH s AS (
SELECT order_id, SUM(amount) AS shipped FROM shipments GROUP BY order_id
), r AS (
SELECT order_id, SUM(amount) AS refunded FROM refunds GROUP BY order_id
), ids AS (
SELECT order_id FROM s UNION SELECT order_id FROM r
)
SELECT ids.order_id, COALESCE(s.shipped, 0) AS shipped,
COALESCE(r.refunded, 0) AS refunded
FROM ids LEFT JOIN s ON s.order_id = ids.order_id
LEFT JOIN r ON r.order_id = ids.order_id;
For the example order the result is $120 and $20. A refund-only order is visible with zero shipped in the chosen query scope. It may have shipped in a prior period. The occurrence dates and filters on the two processes determine that interpretation.
When asked for product-level refunds, check for the allocation evidence before adding product to the grouping. A header-only goodwill credit cannot inherit a product merely because that product shipped in the same order.
The model behind the answer
An order has two dispatches worth $80 and $40. It also has three partial refunds of $5, $7, and $8. Join both tables on order ID and you get six rows. The dispatches now sum to $360 and the refunds to $40. The evidence supports $120 dispatched and $20 refunded. A distinct sum would introduce a different error whenever two legitimate amounts happen to be equal.
For an order-level comparison, group dispatches by order and refunds by order separately, then join the two results. This is drilling across. Preserve orders appearing on only one side when the reporting population requires them. For a product-level comparison, first establish how each refund relates to a line. A header-only goodwill refund has no observed product assignment.
A bus matrix helps at design time. List processes down the side and shared dimensions across the top. Mark “product” only where a consistent product meaning and attribution actually exist. Kimball on multipass queries
Try it in SQLite
Turn $360 back into $120
Two shipments and three refunds for A create six pairs. Order B has only a refund in this reporting period.
Tables and sample data
Each run starts with fresh sample data. Use Ctrl+Enter or ⌘+Enter to run. Only your browser processes the query.
Review a candidate pull request on unfamiliar orders
Give the agent the report grain and let it write the join. Use a one-shipment, one-refund order for its visible example. The reviewer owns a second fixture with two shipments and three refunds, and a third order with a refund only.
Reviewer decision. Ask for per-order rows before reading the query. For A the answer is $120 shipped and $20 refunded. B must remain visible with its $6 refund in the lab. Once the proposed query passes, introduce two genuine shipments for the same amount. A distinct sum now has its own counterexample.
Further reading. The Data Warehouse Toolkit, 3rd ed., on multipass queries for shipments and returns.
One more question
A third table credits two sales reps per order. What happens if you attach it to the repaired order-level totals?
Check your reasoning
Full credit duplicates the totals across reps. A bridge is appropriate for involvement reporting. For additive rep contribution, apply approved weights per order and check conservation. Repairing shipment-to-refund cardinality solved just one of the two joins.
Previous · Reproduce the report before the correction · Next · Allocate one carrier bill to two lines · Complete series map