I want to create an Effectivity Satellite off the link table below ( you can open and create in http://sqlfiddle.com/ via the code below).
CREATE TABLE l_opportunity_account (
opportunity_account_hash_id VARCHAR(100),
load_timestamp TIMESTAMP,
record_source VARCHAR(100),
opportunity_hash_id VARCHAR(100),
account_hash_id VARCHAR(100),
insert_dt TIMESTAMP,
opportunity_account_hashdiff VARCHAR(100)
);
INSERT INTO l_opportunity_account VALUES
('a1', '2023-03-18 02:28:34', 'account', 'a', '1', '2023-03-18', '06D6B4A1E291EFF41128B1871C8749002109354E4A8895FDD647572B631B22E1'),
('b2', '2023-03-18 00:00:00', 'account', 'b', '2', '2023-03-18', 'B4F87912A5D1DB9BC0AAD06428F66F5B92BC3CFC59A36D886CD262D899E5F8D7'),
('c3', '2023-03-18 02:28:34.000' ,'account', 'c','3', '2023-03-18','3DC600B94F4FEC867FB6BF7715B84CA08E46293AFECAB336ED186B6A4351136F'),
('c5', '2023-03-18 02:28:34.000' ,'account', 'c','5', '2023-03-19','3cC600B94F4FEC867FB6BF7715B84CA08E46293AFECAB336ED186B6A4351136F'),
('c3', '2023-03-18 02:28:34.000' ,'account', 'c','3', '2023-03-20','37C600B94F4FEC867FB6BF7715B84CA08E46293AFECAB336ED186B6A4351136F'),
('b1', '2023-03-18 02:28:34.000' ,'account', 'c','1', '2023-03-24','37C600B94F4FEC867FB6BF7715B84CA08E46293AFECAB336ED186B6A4351136F')
The effectivity Satellite has to meet the criteria below:
- Relationships can be 1-many, many-many, 1-1 (so child account can have one or many parent orgs and so on)
- Captures relationships that may have changed (child account went to one parent account after another and then back)
- Contain effective_from & effective_to date for each record
I tried to query a query as below, and the window function does create effective_from & effective_to ---- but does not handle many-many or if a relationship comes back. Do I have columns with the right detail to even do this-- not not what do I need, or can I create an effectivity_sat off this link table above?
WITH es_opportunity_account as (
SELECT
t1.*,
t1.load_timestamp AS effective_from,
LEAD(t1.load_timestamp) OVER (
PARTITION BY t1.opportunity_hash_id
ORDER BY
t1.load_timestamp
) AS effective_to
FROM
l_opportunity_account t1
)
SELECT
*
FROM
es_opportunity_account;