Let's say I have the following target table:
CREATE TABLE DimCustomer (
CustomerKey serial PRIMARY KEY,
CustomerNum int NOT NULL,
CustomerName varchar(25) NOT NULL,
Planet varchar(25) NOT NULL,
RowIsCurrent char(1) NOT NULL DEFAULT 'Y',
RowStartDate date NOT NULL DEFAULT CURRENT_TIMESTAMP,
RowEndDate date NOT NULL DEFAULT '12/31/9999'
);
INSERT INTO DimCustomer
(CustomerNum, CustomerName, Planet, RowStartDate)
VALUES (101,'Anakin Skywalker', 'Tatooine', CURRENT_TIMESTAMP - INTERVAL '101 days'),
(102,'Yoda', 'Coruscant', CURRENT_TIMESTAMP - INTERVAL '100 days'),
(103,'Obi-Wan Kenobi', 'Coruscant', CURRENT_TIMESTAMP - INTERVAL '100 days')
And I have a following staging table:
CREATE TABLE Staging_DimCustomer
(
CustomerNum int NOT NULL,
CustomerName varchar(25) NOT NULL,
Planet varchar(25) NOT NULL,
ChangeDate date NOT NULL DEFAULT CURRENT_TIMESTAMP,
RankNo int NOT NULL DEFAULT 1
)
INSERT INTO Staging_DimCustomer(CustomerNum, CustomerName, Planet, ChangeDate)
VALUES
(103,'Ben Kenobi', 'Coruscant', CURRENT_TIMESTAMP - INTERVAL '99 days')
In the staging table, it looks like 'Obi-Wan Kenobi'(customernum 103) changed his name to
'Ben Kenobi'. I want to create a script that implements scd type 2 and produces the following result(slowly changing dimension type 2):
Following is my attempt:
INSERT INTO DimCustomer (
CustomerNum, CustomerName, Planet, RowIsCurrent, RowStartDate, RowEndDate
)
select CustomerNum, CustomerName, Planet, 'Y', ChangeDate, '12/31/9999'
from Staging_DimCustomer
ON CONFLICT (CustomerNum) and RowIsCurrent = 'Y'
DO UPDATE SET
CustomerName = EXCLUDED.CustomerName,
Planet = EXCLUDED.Planet,
RowIsCurrent = 'N',
RowEndDate = EXCLUDED.ChangeDate
I dont know how to look for the values that changed, update the existing rows to retire it and then insert the new rows with rowiscurrent = 'Y' flag. I am trying to model my solution based on this sql server article
http://www.made2mentor.com/2013/08/how-to-load-slowly-changing-dimensions-using-t-sql-merge/.
