runtime
CRM + ERP Integration · milestone 11 / 12

milestone 11/12 Protect fact grain when joining versions

Join historical versions without multiplying fact rows.

CRM + ERP Integration · Follow extraction reconciliation, authoritative integration, date quarantine, quality gates, stable keys, replay-safe loads, and atomic promotion. Finish with a customer-grain capstone and field-level lineage.

approach

The fact grain is one sales line. Joining a customer to every historical version multiplies rows and totals. Choose the current version for current-state reporting or effective version at event time for historical reporting. Validate row counts as well as measures; DISTINCT does not repair a wrong join.

worked-example.sql
SELECT h.country,SUM(f.sales) AS total_sales
FROM fact_sales f
JOIN dim_customer c ON c.customer_key=f.customer_key
JOIN customer_history h
  ON h.customer_id=c.customer_id
  AND h.valid_to IS NULL
GROUP BY h.country;

data for this milestone

customer_history5 columns · 3 rows
version_keyinteger · keycustomer_idtextcountrytextvalid_fromtextvalid_totext
101C001United States2024-01-012025-02-01
102C001Canada2025-02-01NULL
103C002United Kingdom2024-01-01NULL

all 3 rows

dim_customer4 columns · 5 rows
customer_keyinteger · keycustomer_idtextnametextcountrytext
1C001Alice NguyenUnited States
2C002BenUnited Kingdom
3C003CaraUnited Kingdom
4C004DevIndia
5C005ElenaCanada

all 5 rows

challenge 1

Return country,total_sales using CURRENT customer_history version, for facts with a customer history match.

Group by current country.

what to return

Columns
countrytotal_sales in this order
Rows
any order
challenge-1.sql
 
3 steps left · check both queries and pass the concept checkOpen the reportNext milestone
Protect fact grain when joining versions — CRM + ERP Integration · Runtime