In two sentences
Customers move, products change category, prices change: a slowly changing dimension decides what a table keeps when that happens. Type 1 overwrites the old value; type 2 keeps every version with the dates it was valid, so you can ask what was true at any point in time.
See it
The dimension on 01-01, then a snapshot on 04-01 where c1 moved to Germany and c3 is new:
| customer_id | country | valid_from | valid_to | is_current |
|---|---|---|---|---|
| c1 | FR | 01-01 | 04-01 | false |
| c1 | DE | 04-01 | NULL | true |
| c2 | US | 01-01 | NULL | true |
| c3 | ES | 04-01 | NULL | true |
c1's French version was closed, a German version opened. c2 didn't change, so nothing happened to it.
| type 1 | type 2 | |
|---|---|---|
| on a change | overwrite | close the current row, add a new one |
| rows per customer | 1 | 1 per version |
| "where did c1 live in February?" | can't tell | FR |
| sales by country, as of the sale | wrong for movers | right |
In SQL
Type 2 in two idempotent steps. First, close the current version of every customer whose attributes changed:
MERGE INTO dim_customer t
USING snapshot s
ON t.customer_id = s.customer_id AND t.is_current
WHEN MATCHED AND t.country IS DISTINCT FROM s.country THEN
UPDATE SET valid_to = s.snapshot_date, is_current = false;Then, open a version for every customer without a current one, the changed ones and the new ones:
INSERT INTO dim_customer
SELECT s.customer_id, s.country, s.snapshot_date, NULL, true
FROM snapshot s
WHERE NOT EXISTS (
SELECT 1 FROM dim_customer d WHERE d.customer_id = s.customer_id AND d.is_current
);Run both again with the same snapshot and nothing changes. If the job dies between the two, the next run opens the missing versions.
Where did c1 live on 02-15? valid_to is exclusive:
SELECT country FROM dim_customer
WHERE customer_id = 'c1'
AND valid_from <= DATE '2026-02-15'
AND (valid_to > DATE '2026-02-15' OR valid_to IS NULL); -- FRWhen to use it
| attribute | type |
|---|---|
| corrections (a typo in a name) | 1: the old value was never true |
| attributes reports group by over time (country, segment, plan, category) | 2 |
| attributes nobody reports on historically (phone number) | 1 |
| prices, contracts, anything with legal or money consequences | 2, sometimes bitemporal (see below) |
Best practices
- Decide per column, not per table. A customer table is often type 2 on
countryandsegmentand type 1 onemail. - Use a surrogate key for each version (see Grain, facts and dimensions): facts point at the version that was current when they happened.
- Half-open intervals:
valid_frominclusive,valid_toexclusive. A date then falls in exactly one version. - Compare with
IS DISTINCT FROM, so a change from or to NULL counts as a change. - Decide what a deleted customer means: close the current version, or keep it with a
deletedflag. Don't just leave it current.
Common mistakes
- Closing and opening with the same condition in one MERGE. A MERGE takes one action per target row: it can't close c1's row and insert its new version at the same time. Two steps, or a source that lists c1 twice with different keys.
- Comparing with
<>. A country going from NULL to FR is not "different" to<>. UseIS DISTINCT FROM. - Closed intervals.
valid_to = 04-01andvalid_from = 04-01withBETWEENmatches both versions on 04-01. - Using the load date as
valid_fromwhen the source says when it changed. A change loaded a week late gets the wrong history. Use the source's effective date if it has one; that's the start of bitemporal modelling.
What an interviewer asks next
- "Type 1 or type 2 for customer address?" Depends on whether reports need the address at the time of the order; usually type 2 for country or region, type 1 for the street.
- "How do you join a fact to a type 2 dimension?" On the surrogate key stored in the fact, or on the natural key
plus the fact's date between
valid_fromandvalid_to(see Range joins and as-of joins).
Go further
Kimball's pages on type 1 and type 2 are the original definitions, one screen each.
Practise it
Upsert Daily Customer Changes (SCD Type 1), then Customer History as a Type 2 Slowly Changing Dimension, then Backdated Price Corrections in an SCD2 History (hard).