SQL - 在历史数据上实施 SCD 类型 2?

SQL - implement SCD type 2 on historical data?

我的任务是将系统数据导入我们的 DW 并在地址 Dimension 上实施 SCD。现在 SCD 类型 2 向前发展相对容易(我正在使用 MERGE 语句来执行此操作)但是有些记录可以追溯到几年前,我真的不知道如何处理。例子如下..

ID           Created          HouseNumber          Address         Postcode
5563        01-03-2016            55                court           m37 7hh
5563        06-08-2020            65                high rd         sk7 7hy
2678        23-04-2017            2                 test            juh shh
2678        11-02-2021            1                 new rd          tes tes

我的输出应该如下所示..

ID      Number          Address         Postcode        From           To             Latest   
5563    55                court           m37 7hh        01-03-2016     06-08-2020      0
5563    65                high rd         sk7 7hy        06-08-2020     31-12-9999      1
2678    2                 test            juh shh        23-04-2017     11-02-2021      0
2678    1                 new rd          tes tes        11-02-2021     31-12-9999      1

有什么想法吗?这只是一个初始加载,然后所有的事情都将使用我的 MERGE 语句处理

select ID, Created, HouseNumber, Address, Postcode,
    Created as FromDate,
    LEAD(Created) over (partition by PK order by Created) as ToDate,
    case when LEAD(Created) over (partition by PK order by Created) is null then 1 else 0 end as Latest
from factTable