Roadmap › ETL & Data Warehousing Concepts › Module: Dimensional Modelling
Lessons in this course (12)

7. Slowly Changing Dimensions (SCD)

Intermediate★ Must knowLesson 7 of 12
TypeBehaviourExample
Type 0Never changes (retain original)Date of birth
Type 1Overwrite — no historyFix a spelling mistake
Type 2New row per change with ValidFrom/ValidTo/IsCurrent — full historyCustomer moves city; past sales stay with old city
Type 3Extra column for previous value — limited historyCurrentRegion, PreviousRegion
Type 4History in a separate tableMini-dimension for rapidly changing attributes
Type 61 + 2 + 3 hybridCurrent value on every historical row + full history
sql
-- SCD Type 2 with T-SQL (expire changed rows, then insert new versions)
UPDATE d SET d.ValidTo = CAST(GETDATE() AS DATE), d.IsCurrent = 0
FROM dw.DimCustomer d
JOIN stg.Customer s ON s.CustomerId = d.CustomerId
WHERE d.IsCurrent = 1 AND (d.City <> s.City OR d.Name <> s.Name);

INSERT INTO dw.DimCustomer (CustomerId, Name, City, ValidFrom, ValidTo, IsCurrent)
SELECT s.CustomerId, s.Name, s.City, CAST(GETDATE() AS DATE), '9999-12-31', 1
FROM stg.Customer s
LEFT JOIN dw.DimCustomer d ON d.CustomerId = s.CustomerId AND d.IsCurrent = 1
WHERE d.CustomerKey IS NULL;      -- new customers + customers whose current row was just expired

Fact loads must look up the surrogate key valid at the transaction date (or the current row for new facts).