> ## Content Index
> Fetch the complete content index at: https://abundance.alloconomy.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Compare shipments and refunds without multiplying either
- URL: https://abundance.alloconomy.com/alloconomy/de-interview-compare-shipments-and-refunds-without-multiplying-either/
- Published: 2026-11-22T05:08:09.000Z
- Updated: 2026-11-22T16:31:32.000Z
- Description: An order has two dispatches worth $80 and $40 and three refunds of $5, $7, and $8.
- Author: Sathya Narayanan
- Tags: Alloconomy, Data Engineering, Data Engineering Interview Field Guide, #web-only, #Import 2026-11-21 19:24, Combining facts and reconciling measures, #series-detail

1. [Collection · 10 seriesThe Data Engineering Interview Field Guide](https://abundance.alloconomy.com/alloconomy/data-engineering-interview-field-guide/)
2. [Series opening · 3 essaysCombining facts and reconciling measuresSeries 6 of 10](https://abundance.alloconomy.com/alloconomy/de-interview-compare-shipments-and-refunds-without-multiplying-either/)
3. This essay1 of 3 · Opening

Data engineering · 3 essays

Combining facts and reconciling measures

Complete reading path · You are in Part 1 of 3
1. Part 1**Compare shipments and refunds without multiplying either**Current essayAn order has two dispatches worth $80 and $40 and three refunds of $5, $7, and $8.
2. Part 2[Allocate one carrier bill to two lines](https://abundance.alloconomy.com/alloconomy/de-interview-allocate-one-carrier-bill-to-two-lines/)Three eligible order lines share a $10 carrier charge equally.
3. Part 3[Give a correction somewhere to go](https://abundance.alloconomy.com/alloconomy/de-interview-give-a-correction-somewhere-to-go/)May’s shipment report estimated $12 of freight.

## 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.

```sql
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](https://www.kimballgroup.com/data-warehouse-business-intelligence-resources/kimball-techniques/dimensional-modeling-techniques/multipass-sql/?ref=abundance.alloconomy.com)

## 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 dataExample query Your SQL

Run SQLStopReset example

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

Label Measure 

## 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](https://www.amazon.com/dp/1118530802?ref=abundance.alloconomy.com), 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.

1. [Back to the series openingCombining facts and reconciling measuresSeries 6 of 10](https://abundance.alloconomy.com/alloconomy/de-interview-compare-shipments-and-refunds-without-multiplying-either/)
2. [Explore the collectionThe Data Engineering Interview Field Guide](https://abundance.alloconomy.com/alloconomy/data-engineering-interview-field-guide/)

[Previous · Reproduce the report before the correction](https://abundance.alloconomy.com/alloconomy/de-interview-reproduce-the-report-before-the-correction/) · [Next · Allocate one carrier bill to two lines](https://abundance.alloconomy.com/alloconomy/de-interview-allocate-one-carrier-bill-to-two-lines/) · [Complete series map](https://abundance.alloconomy.com/alloconomy/data-engineering-interview-field-guide/)