The complete reporting file
The fixture has customers 1,2,3; paid orders O1 for customer 1 and O2 for customer 2; and draft O3 with an unresolved NULL customer. O1 has two line items: two units at 12.00 and one at 30.00. O2 has one unit at 11.00; draft O3 has one at 7.00. Prices are transaction snapshots and implementation amounts use integer minor units. This is an operational billed/settled report, not a determination of financial-statement revenue recognition or actual tax treatment.
Settled payments are 30.00 and 24.00 for O1 and 11.00 for O2. Another O1 payment of 5.00 is pending and excluded by policy. Posted refunds are 6.00 for O1 and 2.00 for O2. Customer 1 has two tags, illustrating a separate one-to-many relationship. The report must retain one row per paid order, show billed, settled and posted-refund totals separately, and reconcile a defined net billed amount. Drafts and unresolved assignments must remain visible in exception checks rather than disappear from the database.
Aggregate each fact at the intended grain
O1 has billed and settled amounts of 54.00 and posted refunds of 6.00, yielding net billed 48.00. O2 has 11.00 billed and settled and 2.00 refunded, yielding 9.00. The paid-order report therefore totals billed 65.00, settled 65.00, refunds 8.00 and net billed 57.00. Draft billed amount 7.00 is separately excluded under the stated status policy.
A naive join of lines, settled payments and refunds on order identifier multiplies O1’s two lines by its two settled payments. Line and payment totals for O1 both become 108.00 and its 6.00 refund is repeated four times, becoming 24.00. Including O2 produces incorrect billed and settled totals of 119.00 and refunds of 26.00. DISTINCT applied to monetary values is not a repair: equal legitimate values can occur in different records. Aggregate each independent fact to order level first.
WITH line_totals AS (
SELECT order_id, SUM(quantity * unit_price_cents) AS billed_cents
FROM order_lines GROUP BY order_id
), payment_totals AS (
SELECT order_id, SUM(amount_cents) AS settled_cents
FROM payments WHERE status = 'settled' GROUP BY order_id
), refund_totals AS (
SELECT order_id, SUM(amount_cents) AS refund_cents
FROM refunds WHERE status = 'posted' GROUP BY order_id
)
SELECT o.order_id, COALESCE(l.billed_cents,0) AS billed_cents,
COALESCE(p.settled_cents,0) AS settled_cents,
COALESCE(r.refund_cents,0) AS refund_cents
FROM orders AS o
LEFT JOIN line_totals AS l ON l.order_id = o.order_id
LEFT JOIN payment_totals AS p ON p.order_id = o.order_id
LEFT JOIN refund_totals AS r ON r.order_id = o.order_id
WHERE o.status = 'paid'
ORDER BY o.order_id;
A zero fill here is a display rule for a missing aggregated relation under this specific report contract. It does not prove the missing payment or refund is factually zero in every source system. Keep data-quality checks for missing lines, unresolved payments and mismatched amounts.
A correction must be atomic, replay-safe and auditable
A new authorized synthetic refund command R3 requests 4.00 against O1. Apply the command record and refund record within one transaction, check the permitted amount against the stated settled/refunded balance, and use a stable command identity with a canonical payload. Replaying R3 with the identical payload returns the earlier result without creating another refund. Reusing R3 for 5.00 is a collision and must fail rather than silently alter history. An excessive refund must roll back both the proposed refund and its command marker.
After valid R3, posted refunds total 12.00 and net billed becomes 53.00. O1’s order, line prices and settled payments remain unchanged. An identical retry leaves the same total. The laboratory also tests an invalid oversized command, verifies no partial command record remains, and compares preserved orders and payments. These are ordinary synthetic transaction tests, not operations on Academy learner records or live payment systems.
The cumulative response must show the relational model, keys and nullable relationships; reporting population and status policy; grain of every aggregate; correct joins and row counts; transaction and replay behavior; preserved historical prices; reconciliation before and after the correction; and the limits of the operational measure. Explain how to establish a consistent read boundary and how an index experiment would preserve the answer. Study the complete six on-site studios and preserved normalization, migration, window and temporal-query chapters before optional self-assessment; external links are optional. Correct syntax without a trustworthy business interpretation does not meet the rubric. Independent subject review and learner testing remain separate statuses.
Worked example: Reconcile the correct and multiplied answers
The correct paid-order totals in minor units are 6,500 billed, 6,500 settled and 800 refunded. The naive multi-join gives 11,900, 11,900 and 2,600 because it repeats independent child facts. The difference is a row-grain error, not a rounding difference.
Worked example: A retry is different from a changed command
The first valid R3 adds 400 minor units to refunds, changing net billed from 5,700 to 5,300. The identical replay adds nothing. A changed payload with the same identity is rejected and preserves the stored original result.