Problems / SCD2: Build Effective Ranges / Editorial
effective_from
changed_at
LEAD(changed_at) OVER (PARTITION BY customer_id ORDER BY changed_at)
SCD Type-2 tables look elaborate — paired dates, current flags — but when the source is an ordered change log, the whole structure is one window function: intervals are just consecutive events zipped together, and LEAD/LAG are how SQL zips. Two properties come free and are worth checking on any SCD build: the intervals *tile* (each row's end = next row's start — no gaps or overlaps for point-in-time lookups to fall into), and re-entering a previous state yields distinct stints because the grain is the *change*, not the (customer, plan) pair. When you later need "the plan as of time T", this table answers with effective_from <= T AND (T < effective_to OR effective_to IS NULL).
LEAD
LAG
effective_from <= T AND (T < effective_to OR effective_to IS NULL)
Solve SCD2: Build Effective Ranges yourself →