Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CREATE TABLE DimCustomer (
- CustomerKey serial PRIMARY KEY,
- CustomerNum int unique,
- 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')
- CREATE TABLE Staging_DimCustomer
- (
- CustomerNum int unique,
- 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')
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement