Introduction
In this blogpost, I would like to discuss effectivity satellites and particularly in an "Insert only" Datavault 2.0 architecture. You don't want to update in a Datavault 2.0 architecture because updates are expensive (performance). In this blogpost, I'll describe the effectivity satellite logic as described in "Patrick Cuba's book : The Data Vault Guru : a pragmatic guide on building a data vault". A great book with a lot of examples. I used a couple of his queries and tweaked them a bit for this blogpost.
The code for the effectivity satellite is designed for Snowflake.
The case described
"A car can only be driven by one person and a person can drive multiple cars"
So, in 3NF you would draw this diagram:
These are the testsituations I've used for building the effectivity satellite in Snowflake
Step 0 : Starting situation effectivity Satellite
We start with a initial situation. There are three cars with 3 different drivers.
This is the Snowflake code :
SET LOADDATE = '2023-09-01'; CREATE OR REPLACE TABLE STAGE_DRIVERCARTABLE ( DRIVER_CODE VARCHAR(100), CAR_CODE VARCHAR(100) ); INSERT INTO STAGE_DRIVERCARTABLE VALUES ('A', '1'), ('B', '2'), ('C', '3'); CREATE OR REPLACE TABLE HUB_DRIVER ( DRIVER_HK VARCHAR(100), DRIVER_CODE VARCHAR(100), LOADDTS TIMESTAMP, RECSRC VARCHAR(100) ); INSERT INTO HUB_DRIVER SELECT 'HASH(' || DRIVER_CODE || ')' AS DRIVER_HK, DRIVER_CODE, $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE DRIVER_HK NOT IN (SELECT DRIVER_HK FROM HUB_DRIVER); CREATE OR REPLACE TABLE HUB_CAR ( CAR_HK VARCHAR(100), CAR_CODE VARCHAR(100), LOADDTS TIMESTAMP, RECSRC VARCHAR(100) ); INSERT INTO HUB_CAR SELECT 'HASH(' || CAR_CODE || ')' AS CAR_KEY, CAR_CODE, $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE CAR_KEY NOT IN (SELECT CAR_HK FROM HUB_CAR); CREATE OR REPLACE TABLE LINK_DRIVER_CAR ( DRIVER_CAR_HK VARCHAR(100), DRIVER_HK VARCHAR(100), CAR_HK VARCHAR(100), LOADDTS TIMESTAMP, RECSRC VARCHAR(100) ); INSERT INTO LINK_DRIVER_CAR SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS DRIVER_CAR_KEY, 'HASH(' || DRIVER_CODE || ')', 'HASH(' || CAR_CODE || ')', $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE DRIVER_CAR_KEY NOT IN (SELECT DRIVER_CAR_HK FROM LINK_DRIVER_CAR); CREATE OR REPLACE TABLE ESAT_DRIVER_CAR ( DRIVER_CAR_HK VARCHAR(100), EFFECTIVE_FROM TIMESTAMP, EFFECTIVE_TO TIMESTAMP, LOADDTS TIMESTAMP, RECSRC VARCHAR(100) ); INSERT INTO ESAT_DRIVER_CAR WITH CTE_CURRENT_EFFECTIVITY AS ( SELECT SUBQ1.DRIVER_CAR_HK, SUBQ1.EFFECTIVE_FROM, SUBQ1.EFFECTIVE_TO, SUBQ1.LOADDTS FROM ( SELECT ESAT.DRIVER_CAR_HK, ESAT.EFFECTIVE_FROM, ESAT.EFFECTIVE_TO, ESAT.LOADDTS, RANK() OVER (PARTITION BY ESAT.DRIVER_CAR_HK ORDER BY ESAT.EFFECTIVE_FROM, ESAT.LOADDTS DESC) AS DV_RANK FROM ESAT_DRIVER_CAR ESAT) SUBQ1 INNER JOIN LINK_DRIVER_CAR LNK ON SUBQ1.DRIVER_CAR_HK = LNK.DRIVER_CAR_HK WHERE DV_RANK = 1 AND EFFECTIVE_TO = TO_TIMESTAMP_NTZ('9999-12-31') ), CTE_NEW_EFFECTIVITY AS( SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS DRIVER_CAR_KEY, $LOADDATE AS EFFECTIVE_FROM, '9999-12-31' AS EFFECTIVE_TO, $LOADDATE AS LOADDTS, 'CARSYSTEM' AS RECSRC FROM STAGE_DRIVERCARTABLE WHERE DRIVER_CAR_KEY NOT IN (SELECT DRIVER_CAR_HK FROM CTE_CURRENT_EFFECTIVITY) ), CTE_DELETE_EFFECTIVITY AS ( SELECT ESAT.DRIVER_CAR_HK AS DRIVER_CAR_KEY, LAST_VALUE(ESAT.EFFECTIVE_FROM) OVER (PARTITION BY ESAT.DRIVER_CAR_HK ORDER BY ESAT.EFFECTIVE_FROM, ESAT.LOADDTS DESC) AS EFFECTIVE_FROM, $LOADDATE AS EFFECTIVE_TO, $LOADDATE AS LOADDTS, 'CARSYSTEM' AS RECSRC FROM CTE_CURRENT_EFFECTIVITY ESAT WHERE DRIVER_CAR_KEY NOT IN ( SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS STG_DRIVER_CAR_KEY FROM STAGE_DRIVERCARTABLE ) ) SELECT DRIVER_CAR_KEY AS DRIVER_CAR_HK, EFFECTIVE_FROM, EFFECTIVE_TO, LOADDTS, RECSRC FROM CTE_NEW_EFFECTIVITY UNION ALL SELECT DRIVER_CAR_KEY AS DRIVER_CAR_HK, EFFECTIVE_FROM, EFFECTIVE_TO, LOADDTS, RECSRC FROM CTE_DELETE_EFFECTIVITY;
And this will result in the following effectivity satellite table :
So this is the starting situation. Now, continue with the next step.
Situation 1 : Driver A owns now a new car with VIN 4
SET LOADDATE = '2023-09-02'; CREATE OR REPLACE TABLE STAGE_DRIVERCARTABLE ( DRIVER_CODE VARCHAR(100), CAR_CODE VARCHAR(100) ); INSERT INTO STAGE_DRIVERCARTABLE VALUES ('A', '1'), ('B', '2'), ('C', '3'), ('A', '4'); INSERT INTO HUB_DRIVER SELECT 'HASH(' || DRIVER_CODE || ')' AS DRIVER_HK, DRIVER_CODE, $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE DRIVER_HK NOT IN (SELECT DRIVER_HK FROM HUB_DRIVER); INSERT INTO HUB_CAR SELECT 'HASH(' || CAR_CODE || ')' AS CAR_KEY, CAR_CODE, $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE CAR_KEY NOT IN (SELECT CAR_HK FROM HUB_CAR); INSERT INTO LINK_DRIVER_CAR SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS DRIVER_CAR_KEY, 'HASH(' || DRIVER_CODE || ')', 'HASH(' || CAR_CODE || ')', $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE DRIVER_CAR_KEY NOT IN (SELECT DRIVER_CAR_HK FROM LINK_DRIVER_CAR); INSERT INTO ESAT_DRIVER_CAR WITH CTE_CURRENT_EFFECTIVITY AS ( SELECT SUBQ1.DRIVER_CAR_HK, SUBQ1.EFFECTIVE_FROM, SUBQ1.EFFECTIVE_TO, SUBQ1.LOADDTS FROM ( SELECT ESAT.DRIVER_CAR_HK, ESAT.EFFECTIVE_FROM, ESAT.EFFECTIVE_TO, ESAT.LOADDTS, RANK() OVER (PARTITION BY ESAT.DRIVER_CAR_HK ORDER BY ESAT.EFFECTIVE_FROM, ESAT.LOADDTS DESC) AS DV_RANK FROM ESAT_DRIVER_CAR ESAT) SUBQ1 INNER JOIN LINK_DRIVER_CAR LNK ON SUBQ1.DRIVER_CAR_HK = LNK.DRIVER_CAR_HK WHERE DV_RANK = 1 AND EFFECTIVE_TO = TO_TIMESTAMP_NTZ('9999-12-31') ), CTE_NEW_EFFECTIVITY AS( SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS DRIVER_CAR_KEY, $LOADDATE AS EFFECTIVE_FROM, '9999-12-31' AS EFFECTIVE_TO, $LOADDATE AS LOADDTS, 'CARSYSTEM' AS RECSRC FROM STAGE_DRIVERCARTABLE WHERE DRIVER_CAR_KEY NOT IN (SELECT DRIVER_CAR_HK FROM CTE_CURRENT_EFFECTIVITY) ), CTE_DELETE_EFFECTIVITY AS ( SELECT ESAT.DRIVER_CAR_HK AS DRIVER_CAR_KEY, LAST_VALUE(ESAT.EFFECTIVE_FROM) OVER (PARTITION BY ESAT.DRIVER_CAR_HK ORDER BY ESAT.EFFECTIVE_FROM, ESAT.LOADDTS DESC) AS EFFECTIVE_FROM, $LOADDATE AS EFFECTIVE_TO, $LOADDATE AS LOADDTS, 'CARSYSTEM' AS RECSRC FROM CTE_CURRENT_EFFECTIVITY ESAT WHERE DRIVER_CAR_KEY NOT IN ( SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS STG_DRIVER_CAR_KEY FROM STAGE_DRIVERCARTABLE ) ) SELECT DRIVER_CAR_KEY AS DRIVER_CAR_HK, EFFECTIVE_FROM, EFFECTIVE_TO, LOADDTS, RECSRC FROM CTE_NEW_EFFECTIVITY UNION ALL SELECT DRIVER_CAR_KEY AS DRIVER_CAR_HK, EFFECTIVE_FROM, EFFECTIVE_TO, LOADDTS, RECSRC FROM CTE_DELETE_EFFECTIVITY;
And this results in the following effectivity satellite table :
Now continue to a more complicated situation.
Step 2 : Driver B doesn’t drive Car with VIN 2 anymore
Here is the sample code :
SET LOADDATE = '2023-09-03'; CREATE OR REPLACE TABLE STAGE_DRIVERCARTABLE ( DRIVER_CODE VARCHAR(100), CAR_CODE VARCHAR(100) ); INSERT INTO STAGE_DRIVERCARTABLE VALUES ('A', '1'), --('B', '2'), ('C', '3'), ('A', '4'); INSERT INTO HUB_DRIVER SELECT 'HASH(' || DRIVER_CODE || ')' AS DRIVER_HK, DRIVER_CODE, $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE DRIVER_HK NOT IN (SELECT DRIVER_HK FROM HUB_DRIVER); INSERT INTO HUB_CAR SELECT 'HASH(' || CAR_CODE || ')' AS CAR_KEY, CAR_CODE, $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE CAR_KEY NOT IN (SELECT CAR_HK FROM HUB_CAR); INSERT INTO LINK_DRIVER_CAR SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS DRIVER_CAR_KEY, 'HASH(' || DRIVER_CODE || ')', 'HASH(' || CAR_CODE || ')', $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE DRIVER_CAR_KEY NOT IN (SELECT DRIVER_CAR_HK FROM LINK_DRIVER_CAR); INSERT INTO ESAT_DRIVER_CAR WITH CTE_CURRENT_EFFECTIVITY AS ( SELECT SUBQ1.DRIVER_CAR_HK, SUBQ1.EFFECTIVE_FROM, SUBQ1.EFFECTIVE_TO, SUBQ1.LOADDTS FROM ( SELECT ESAT.DRIVER_CAR_HK, ESAT.EFFECTIVE_FROM, ESAT.EFFECTIVE_TO, ESAT.LOADDTS, RANK() OVER (PARTITION BY ESAT.DRIVER_CAR_HK ORDER BY ESAT.EFFECTIVE_FROM, ESAT.LOADDTS DESC) AS DV_RANK FROM ESAT_DRIVER_CAR ESAT) SUBQ1 INNER JOIN LINK_DRIVER_CAR LNK ON SUBQ1.DRIVER_CAR_HK = LNK.DRIVER_CAR_HK WHERE DV_RANK = 1 AND EFFECTIVE_TO = TO_TIMESTAMP_NTZ('9999-12-31') ), CTE_NEW_EFFECTIVITY AS( SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS DRIVER_CAR_KEY, $LOADDATE AS EFFECTIVE_FROM, '9999-12-31' AS EFFECTIVE_TO, $LOADDATE AS LOADDTS, 'CARSYSTEM' AS RECSRC FROM STAGE_DRIVERCARTABLE WHERE DRIVER_CAR_KEY NOT IN (SELECT DRIVER_CAR_HK FROM CTE_CURRENT_EFFECTIVITY) ), CTE_DELETE_EFFECTIVITY AS ( SELECT ESAT.DRIVER_CAR_HK AS DRIVER_CAR_KEY, LAST_VALUE(ESAT.EFFECTIVE_FROM) OVER (PARTITION BY ESAT.DRIVER_CAR_HK ORDER BY ESAT.EFFECTIVE_FROM, ESAT.LOADDTS DESC) AS EFFECTIVE_FROM, $LOADDATE AS EFFECTIVE_TO, -- Wat als we initial load gaan doen? $LOADDATE AS LOADDTS, 'CARSYSTEM' AS RECSRC FROM CTE_CURRENT_EFFECTIVITY ESAT WHERE DRIVER_CAR_KEY NOT IN ( SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS STG_DRIVER_CAR_KEY FROM STAGE_DRIVERCARTABLE ) ) SELECT DRIVER_CAR_KEY AS DRIVER_CAR_HK, EFFECTIVE_FROM, EFFECTIVE_TO, LOADDTS, RECSRC FROM CTE_NEW_EFFECTIVITY UNION ALL SELECT DRIVER_CAR_KEY AS DRIVER_CAR_HK, EFFECTIVE_FROM, EFFECTIVE_TO, LOADDTS, RECSRC FROM CTE_DELETE_EFFECTIVITY;
And here we have the result of the queries (only showing the ESAT table):
Situation 3 : Car with VIN 4 now belongs to Driver B
SET LOADDATE = '2023-09-05'; CREATE OR REPLACE TABLE STAGE_DRIVERCARTABLE ( DRIVER_CODE VARCHAR(100), CAR_CODE VARCHAR(100) ); INSERT INTO STAGE_DRIVERCARTABLE VALUES ('A', '1'), --('B', '2'), ('C', '3'), --('A', '4'), --Changed ('B', '4'); --Changed INSERT INTO HUB_DRIVER SELECT 'HASH(' || DRIVER_CODE || ')' AS DRIVER_HK, DRIVER_CODE, $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE DRIVER_HK NOT IN (SELECT DRIVER_HK FROM HUB_DRIVER); INSERT INTO HUB_CAR SELECT 'HASH(' || CAR_CODE || ')' AS CAR_KEY, CAR_CODE, $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE CAR_KEY NOT IN (SELECT CAR_HK FROM HUB_CAR); INSERT INTO LINK_DRIVER_CAR SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS DRIVER_CAR_KEY, 'HASH(' || DRIVER_CODE || ')', 'HASH(' || CAR_CODE || ')', $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE DRIVER_CAR_KEY NOT IN (SELECT DRIVER_CAR_HK FROM LINK_DRIVER_CAR); INSERT INTO ESAT_DRIVER_CAR WITH CTE_CURRENT_EFFECTIVITY AS ( SELECT SUBQ1.DRIVER_CAR_HK, SUBQ1.EFFECTIVE_FROM, SUBQ1.EFFECTIVE_TO, SUBQ1.LOADDTS FROM ( SELECT ESAT.DRIVER_CAR_HK, ESAT.EFFECTIVE_FROM, ESAT.EFFECTIVE_TO, ESAT.LOADDTS, RANK() OVER (PARTITION BY ESAT.DRIVER_CAR_HK ORDER BY ESAT.EFFECTIVE_FROM, ESAT.LOADDTS DESC) AS DV_RANK FROM ESAT_DRIVER_CAR ESAT) SUBQ1 INNER JOIN LINK_DRIVER_CAR LNK ON SUBQ1.DRIVER_CAR_HK = LNK.DRIVER_CAR_HK WHERE DV_RANK = 1 AND EFFECTIVE_TO = TO_TIMESTAMP_NTZ('9999-12-31') ), CTE_NEW_EFFECTIVITY AS( SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS DRIVER_CAR_KEY, $LOADDATE AS EFFECTIVE_FROM, '9999-12-31' AS EFFECTIVE_TO, $LOADDATE AS LOADDTS, 'CARSYSTEM' AS RECSRC FROM STAGE_DRIVERCARTABLE WHERE DRIVER_CAR_KEY NOT IN (SELECT DRIVER_CAR_HK FROM CTE_CURRENT_EFFECTIVITY) ), CTE_DELETE_EFFECTIVITY AS ( SELECT ESAT.DRIVER_CAR_HK AS DRIVER_CAR_KEY, LAST_VALUE(ESAT.EFFECTIVE_FROM) OVER (PARTITION BY ESAT.DRIVER_CAR_HK ORDER BY ESAT.EFFECTIVE_FROM, ESAT.LOADDTS DESC) AS EFFECTIVE_FROM, $LOADDATE AS EFFECTIVE_TO, -- Wat als we initial load gaan doen? $LOADDATE AS LOADDTS, 'CARSYSTEM' AS RECSRC FROM CTE_CURRENT_EFFECTIVITY ESAT WHERE DRIVER_CAR_KEY NOT IN ( SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS STG_DRIVER_CAR_KEY FROM STAGE_DRIVERCARTABLE ) ) SELECT DRIVER_CAR_KEY AS DRIVER_CAR_HK, EFFECTIVE_FROM, EFFECTIVE_TO, LOADDTS, RECSRC FROM CTE_NEW_EFFECTIVITY UNION ALL SELECT DRIVER_CAR_KEY AS DRIVER_CAR_HK, EFFECTIVE_FROM, EFFECTIVE_TO, LOADDTS, RECSRC FROM CTE_DELETE_EFFECTIVITY;
And here we have the result of the effectivity satellite :
Situation 4 : Driver C doesn’t drive car with VIN 3 but VIN 5
SET LOADDATE = '2023-09-10'; CREATE OR REPLACE TABLE STAGE_DRIVERCARTABLE ( DRIVER_CODE VARCHAR(100), CAR_CODE VARCHAR(100) ); INSERT INTO STAGE_DRIVERCARTABLE VALUES ('A', '1'), --('B', '2'), --('C', '3'), --Changed --('A', '4'), ('B', '4'), ('C', '5'); --Changed INSERT INTO HUB_DRIVER SELECT 'HASH(' || DRIVER_CODE || ')' AS DRIVER_HK, DRIVER_CODE, $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE DRIVER_HK NOT IN (SELECT DRIVER_HK FROM HUB_DRIVER); INSERT INTO HUB_CAR SELECT 'HASH(' || CAR_CODE || ')' AS CAR_KEY, CAR_CODE, $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE CAR_KEY NOT IN (SELECT CAR_HK FROM HUB_CAR); INSERT INTO LINK_DRIVER_CAR SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS DRIVER_CAR_KEY, 'HASH(' || DRIVER_CODE || ')', 'HASH(' || CAR_CODE || ')', $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE DRIVER_CAR_KEY NOT IN (SELECT DRIVER_CAR_HK FROM LINK_DRIVER_CAR); INSERT INTO ESAT_DRIVER_CAR WITH CTE_CURRENT_EFFECTIVITY AS ( SELECT SUBQ1.DRIVER_CAR_HK, SUBQ1.EFFECTIVE_FROM, SUBQ1.EFFECTIVE_TO, SUBQ1.LOADDTS FROM ( SELECT ESAT.DRIVER_CAR_HK, ESAT.EFFECTIVE_FROM, ESAT.EFFECTIVE_TO, ESAT.LOADDTS, RANK() OVER (PARTITION BY ESAT.DRIVER_CAR_HK ORDER BY ESAT.EFFECTIVE_FROM, ESAT.LOADDTS DESC) AS DV_RANK FROM ESAT_DRIVER_CAR ESAT) SUBQ1 INNER JOIN LINK_DRIVER_CAR LNK ON SUBQ1.DRIVER_CAR_HK = LNK.DRIVER_CAR_HK WHERE DV_RANK = 1 AND EFFECTIVE_TO = TO_TIMESTAMP_NTZ('9999-12-31') ), CTE_NEW_EFFECTIVITY AS( SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS DRIVER_CAR_KEY, $LOADDATE AS EFFECTIVE_FROM, '9999-12-31' AS EFFECTIVE_TO, $LOADDATE AS LOADDTS, 'CARSYSTEM' AS RECSRC FROM STAGE_DRIVERCARTABLE WHERE DRIVER_CAR_KEY NOT IN (SELECT DRIVER_CAR_HK FROM CTE_CURRENT_EFFECTIVITY) ), CTE_DELETE_EFFECTIVITY AS ( SELECT ESAT.DRIVER_CAR_HK AS DRIVER_CAR_KEY, LAST_VALUE(ESAT.EFFECTIVE_FROM) OVER (PARTITION BY ESAT.DRIVER_CAR_HK ORDER BY ESAT.EFFECTIVE_FROM, ESAT.LOADDTS DESC) AS EFFECTIVE_FROM, $LOADDATE AS EFFECTIVE_TO, -- Wat als we initial load gaan doen? $LOADDATE AS LOADDTS, 'CARSYSTEM' AS RECSRC FROM CTE_CURRENT_EFFECTIVITY ESAT WHERE DRIVER_CAR_KEY NOT IN ( SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS STG_DRIVER_CAR_KEY FROM STAGE_DRIVERCARTABLE ) ) SELECT DRIVER_CAR_KEY AS DRIVER_CAR_HK, EFFECTIVE_FROM, EFFECTIVE_TO, LOADDTS, RECSRC FROM CTE_NEW_EFFECTIVITY UNION ALL SELECT DRIVER_CAR_KEY AS DRIVER_CAR_HK, EFFECTIVE_FROM, EFFECTIVE_TO, LOADDTS, RECSRC FROM CTE_DELETE_EFFECTIVITY;
And the content in the effectivity satellite is depicted below :
The next situation is also interesting because this particular situation can go wrong if you haven't design the queries properly.
Situation 5 : What if a car is back to the original driver ?
SET LOADDATE = '2023-09-15'; CREATE OR REPLACE TABLE STAGE_DRIVERCARTABLE ( DRIVER_CODE VARCHAR(100), CAR_CODE VARCHAR(100) ); INSERT INTO STAGE_DRIVERCARTABLE VALUES ('A', '1'), --('B', '2'), --('C', '3'), ('A', '4'), --Changed --('B', '4'), --Changed ('C', '5'); INSERT INTO HUB_DRIVER SELECT 'HASH(' || DRIVER_CODE || ')' AS DRIVER_HK, DRIVER_CODE, $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE DRIVER_HK NOT IN (SELECT DRIVER_HK FROM HUB_DRIVER); INSERT INTO HUB_CAR SELECT 'HASH(' || CAR_CODE || ')' AS CAR_KEY, CAR_CODE, $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE CAR_KEY NOT IN (SELECT CAR_HK FROM HUB_CAR); INSERT INTO LINK_DRIVER_CAR SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS DRIVER_CAR_KEY, 'HASH(' || DRIVER_CODE || ')', 'HASH(' || CAR_CODE || ')', $LOADDATE, 'CARSYSTEM' FROM STAGE_DRIVERCARTABLE WHERE DRIVER_CAR_KEY NOT IN (SELECT DRIVER_CAR_HK FROM LINK_DRIVER_CAR); INSERT INTO ESAT_DRIVER_CAR WITH CTE_CURRENT_EFFECTIVITY AS ( SELECT SUBQ1.DRIVER_CAR_HK, SUBQ1.EFFECTIVE_FROM, SUBQ1.EFFECTIVE_TO, SUBQ1.LOADDTS FROM ( SELECT ESAT.DRIVER_CAR_HK, ESAT.EFFECTIVE_FROM, ESAT.EFFECTIVE_TO, ESAT.LOADDTS, RANK() OVER (PARTITION BY ESAT.DRIVER_CAR_HK ORDER BY ESAT.EFFECTIVE_FROM, ESAT.LOADDTS DESC) AS DV_RANK FROM ESAT_DRIVER_CAR ESAT) SUBQ1 INNER JOIN LINK_DRIVER_CAR LNK ON SUBQ1.DRIVER_CAR_HK = LNK.DRIVER_CAR_HK WHERE DV_RANK = 1 AND EFFECTIVE_TO = TO_TIMESTAMP_NTZ('9999-12-31') ), CTE_NEW_EFFECTIVITY AS( SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS DRIVER_CAR_KEY, $LOADDATE AS EFFECTIVE_FROM, '9999-12-31' AS EFFECTIVE_TO, $LOADDATE AS LOADDTS, 'CARSYSTEM' AS RECSRC FROM STAGE_DRIVERCARTABLE WHERE DRIVER_CAR_KEY NOT IN (SELECT DRIVER_CAR_HK FROM CTE_CURRENT_EFFECTIVITY) ), CTE_DELETE_EFFECTIVITY AS ( SELECT ESAT.DRIVER_CAR_HK AS DRIVER_CAR_KEY, LAST_VALUE(ESAT.EFFECTIVE_FROM) OVER (PARTITION BY ESAT.DRIVER_CAR_HK ORDER BY ESAT.EFFECTIVE_FROM, ESAT.LOADDTS DESC) AS EFFECTIVE_FROM, $LOADDATE AS EFFECTIVE_TO, -- Wat als we initial load gaan doen? $LOADDATE AS LOADDTS, 'CARSYSTEM' AS RECSRC FROM CTE_CURRENT_EFFECTIVITY ESAT WHERE DRIVER_CAR_KEY NOT IN ( SELECT 'HASH(' || CAR_CODE || DRIVER_CODE || ')' AS STG_DRIVER_CAR_KEY FROM STAGE_DRIVERCARTABLE ) ) SELECT DRIVER_CAR_KEY AS DRIVER_CAR_HK, EFFECTIVE_FROM, EFFECTIVE_TO, LOADDTS, RECSRC FROM CTE_NEW_EFFECTIVITY UNION ALL SELECT DRIVER_CAR_KEY AS DRIVER_CAR_HK, EFFECTIVE_FROM, EFFECTIVE_TO, LOADDTS, RECSRC FROM CTE_DELETE_EFFECTIVITY; /* SELECT * FROM STAGE_DRIVERCARTABLE; SELECT * FROM HUB_DRIVER; SELECT * FROM HUB_CAR; SELECT * FROM LINK_DRIVER_CAR; SELECT * FROM ESAT_DRIVER_CAR ORDER BY LOADDTS; */
And the content of the effectivity satellite is now as follows :
How to get information out of this table?
Now that we have stored all the possible situations that we can imagine, how do we get the information out of this effectivity satellite? There are different ways of looking at : current, point-in-time or history. In this example I'll show the current value for now.
SELECT SUBQ1.DRIVER_CAR_HK, SUBQ1.EFFECTIVE_FROM, SUBQ1.EFFECTIVE_TO, SUBQ1.LOADDTS, SUBQ1.RECSRC FROM ( SELECT ESAT.DRIVER_CAR_HK, ESAT.EFFECTIVE_FROM, ESAT.EFFECTIVE_TO, ESAT.LOADDTS, ESAT.RECSRC, RANK() OVER (PARTITION BY ESAT.DRIVER_CAR_HK ORDER BY ESAT.EFFECTIVE_FROM DESC, ESAT.LOADDTS DESC) AS DV_RANK FROM ESAT_DRIVER_CAR ESAT /*WHERE ESAT.DRIVER_CAR_HK = 'HASH(4A)' */) SUBQ1 INNER JOIN LINK_DRIVER_CAR LNK ON SUBQ1.DRIVER_CAR_HK = LNK.DRIVER_CAR_HK WHERE DV_RANK = 1 AND EFFECTIVE_TO = TO_TIMESTAMP_NTZ('9999-12-31')
Resulting in :
Geen opmerkingen:
Een reactie posten