/*******************************************************************************
*******************************************************************************
* - SQL example for Loading an bitemporal Satellite.
* - See disclaimer.md in the repository root.
******************************************************************************/
All new records, and all records from the target object with overlapping time periods
SELECT […] FROM <NewRecords>
SELECT […] FROM <target data object with overlaps>
, PreserveRightPartOfExistingRecords AS
-- Duplicate the existing record, preserving the right portion
-- Use the new inscription timestamp and record id
,NewRecords.[Inscription_Timestamp]
,NewRecords.[Inscription_Record_Id]
-- Replace the existing state from timestamp with new state before timestamp
,COALESCE(NewRecords.State_Before_Timestamp,'9999-12-31') AS [State_From_Timestamp]
-- Keep the existing state before timestamp
,ExistingRight.[State_Before_Timestamp]
-- Map the available existing data
-- Order the records - Only the most recent record is of interest
OVER (PARTITION BY ExistingRight.[CODE]
,NewRecords.[State_Before_Timestamp] -- New State From Timestamp
ORDER BY ExistingRight.[Inscription_Timestamp] DESC
,ExistingRight.[Inscription_Record_Id] DESC)
ON NewRecords.[CUSTOMER_SK] = ExistingRight.[CUSTOMER_SK]
-- Compare only with previous records
NewRecords.[Inscription_Timestamp] > ExistingRight.[Inscription_Timestamp]
-- Find Allen Relationship (right)
AND COALESCE(NewRecords.State_Before_Timestamp,'9999-12-31') > ExistingRight.[State_From_Timestamp]
AND COALESCE(NewRecords.State_Before_Timestamp,'9999-12-31') < ExistingRight.[State_Before_Timestamp]
SELECT […] FROM <PreserveRightPartOfExistingRecords>
SELECT […] FROM <NewRecords>
SELECT […] FROM <target data object with overlaps>
, PreserveLeftPartOfExistingRecords AS
-- Duplicate the existing record, preserving the left portion
-- Use new inscription timestamp and record id
,NewRecords.[Inscription_Timestamp]
,NewRecords.[Inscription_Record_Id]
-- Keep the existing state before timestamp
,ExistingLeft.[State_From_Timestamp] AS [State_From_Timestamp]
-- Replace the existing state before timestamp with new state from timestamp
,NewRecords.[State_From_Timestamp] AS [State_Before_Timestamp]
-- Map the available existing data
,ExistingLeft. <available columns>
-- Order the records - Only the most recent record is of interest
OVER (PARTITION BY ExistingLeft.[CODE]
,NewRecords.State_From_Timestamp -- New State Before Timestamp
ORDER BY ExistingLeft.[Inscription_Timestamp] DESC
,ExistingLeft.[Inscription_Record_Id] DESC)
ON NewRecords.[CUSTOMER_SK] = ExistingLeft.[CUSTOMER_SK]
-- Compare only with previous records
NewRecords.Inscription_Timestamp > ExistingLeft.Inscription_Timestamp
-- Find Allen Relationship (left)
AND NewRecords.State_From_Timestamp > ExistingLeft.State_From_Timestamp
AND NewRecords.State_From_Timestamp < ExistingLeft.State_Before_Timestamp
INSERT INTO <target data object>
SELECT <PreserveLeftPartOfExistingRecords>
SELECT <PreserveRightPartOfExistingRecords>