Scenario: Reconciling Reports in SQL
Freeze, diff both ways, codify definitions
Revenue differs between reports: freeze definitions (tables, filters, NULL handling, time zone, dedup rule), reproduce side by side, diff row sets (EXCEPT both directions), trace to divergent predicate — usually fan-out double-counting, timezone day boundaries, or NULL inclusion. Metric definitions belong in versioned code (dbt/metrics layer), not tribal knowledge.
Freeze definitions
Tables, filters, zones, NULL rules, dedup keys — written, same both sides.
Reproduce side by side
Same instant, same params — no moving windows between runs.
EXCEPT both ways
A−B plus B−A isolates each side’s extras for tracing.
Codify metrics
Versioned dbt/metrics definitions — tribal knowledge ends.
1-- isolate extras both ways:2(SELECT * FROM report_a EXCEPT SELECT * FROM report_b)3UNION ALL4(SELECT * FROM report_b EXCEPT SELECT * FROM report_a);
| divergence | rank |
|---|---|
| fan-out | usual suspect 1 |
| timezone | suspect 2 |
| NULL rule | suspect 3 |
Bidirectional EXCEPT surfaces rows unique to each report. Fan-out duplicates inflate one side; midnight-boundary zones shift the other; NULL inclusion widens a third.
Bidirectional diff
Each side’s extras surfaced.
1(SELECT * FROM report_a EXCEPT SELECT * FROM report_b)2UNION ALL3(SELECT * FROM report_b EXCEPT SELECT * FROM report_a);
| side | extras |
|---|---|
| a-only | 3 |
| b-only | 1 |
Usual-trio trace order
Fan-out, zones, NULLs — in that order.
1-- 1. fan-out double-count? 2. midnight zones? 3. NULL inclusion?
| suspect | check |
|---|---|
| fan-out | check aggregations post-join |
| zones | check day bounds |
| nulls | check inclusion rules |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.