【问题标题】:SQL - implement SCD type 2 on historical data?SQL - 对历史数据实施 SCD 类型 2?
【发布时间】:2021-08-12 16:46:44
【问题描述】:

我的任务是将系统数据导入我们的 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 语句处理

【问题讨论】:

  • LAGLEAD 窗口函数。

标签: sql sql-server-2016


【解决方案1】:
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

【讨论】:

  • 谢谢,刚刚阅读了 LEAD(以前没有使用过),这真的很有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-18
  • 2020-05-08
  • 1970-01-01
  • 2020-04-22
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
相关资源
最近更新 更多