Slowly Changing Dimensions
Slowly Changing Dimensions (SCD) is a concept describing the way changes are maintained in dimensional tables.
Based on that, we can choose the refresh approach: overwriting, adding a new row or new column, or keeping only up-to-date values in the main table and tracking changes in the history table.
In the following, we will see which refresh approaches correspond to different SCD types, along with use cases and SQL queries for refreshing.
SCD Types:
Type 0 – Fixed
Tables with no updates, where only new rows are being added. For such tables, no changes in existing rows are expected. For example, a customer's date of birth: the ID is automatically generated, and there's no possibility for the user to change their name or date of birth.

MERGE INTO dim_customer d
USING staging_customer s
ON d.customer_id = s.customer_id
WHEN NOT MATCHED THEN
INSERT (customer_id, customer_name, date_of_birth)
VALUES (s.customer_id, s.customer_name, s.date_of_birth);
Type 1 – Overwrite
Existing data is overwritten, and new rows are being added. In this case, only up-to-date values are needed. If there are any changes in the existing rows, we simply overwrite them.

MERGE INTO customer_emails d
USING staging_customer s
ON d.customer_id = s.customer_id
WHEN MATCHED THEN
UPDATE SET
customer_name = s.customer_name,
email = s.email
WHEN NOT MATCHED THEN
INSERT ALL BY NAME;
Type 2 – Add row
In cases where it's important to preserve historical values, we can add changes as new rows. To track which row is up to date, we need additional columns.
We can use a valid or expired flag: add the new row and set the flag for the existing one to 0.

First, expire the current row:
MERGE INTO customer_emails d
USING staging_customer s
ON d.customer_id = s.customer_id
AND d.valid_flg = 1
WHEN MATCHED
AND d.email IS DISTINCT FROM s.email
THEN UPDATE SET
d.valid_flg = 0
WHEN NOT MATCHED THEN
INSERT ALL BY NAME;
Then add a new one:
INSERT INTO customer_emails
SELECT
s.customer_id,
s.name,
s.email,
s.extract_time AS valid_from,
1 as valid_flg
FROM staging_customer s
INNER JOIN customer_emails c
ON c.customer_id = s.customer_id;
Type 3 – Add column
If there is a limited number of changes we're interested in, we can avoid increasing the number of rows by adding new columns for old values. For example, saving the current and previous email for each customer:

MERGE INTO customer_emails d
USING staging_customer s
ON d.customer_id = s.customer_id
WHEN MATCHED
AND d.email IS DISTINCT FROM s.email
THEN UPDATE SET
previous_email = d.email,
email = s.email
WHEN NOT MATCHED THEN
INSERT ALL BY NAME;
Type 4 – History Table
This is a combination of SCD1 and SCD2: keep only up-to-date values in the main table and track all the changes in the history table.
Instead of using a valid flag for the history table, we will add two date columns: valid_from and valid_to. Set the valid_to default value to the maximum possible timestamp.

History table
Expire the existing row in the history table:
MERGE INTO customer_email_history AS d
USING staging_customer AS s
ON d.customer_id = s.customer_id
AND d.valid_to = TO_TIMESTAMP('9999-12-31')
WHEN MATCHED AND (
d.name IS DISTINCT FROM s.name
OR d.email IS DISTINCT FROM s.email
)
THEN UPDATE SET
d.valid_to = s.extract_time
WHEN NOT MATCHED THEN
INSERT ALL BY NAME;
Add a new one:
INSERT INTO customer_email_history
SELECT
s.customer_id,
s.name,
s.email,
s.extract_time AS valid_from,
TO_TIMESTAMP('9999-12-31') AS valid_to
FROM staging_customer s
INNER JOIN customer_email_history c
ON c.customer_id = s.customer_id
AND c.valid_to = s.extract_time
;
Main table
Fully refresh:
CREATE OR REPLACE TABLE current_customer AS
SELECT
customer_id,
name,
email
FROM customer_email_history
WHERE valid_to = TO_TIMESTAMP('9999-12-31');

