【问题标题】:How to fetch data from SCD type2 table for particular year如何从 SCD type2 表中获取特定年份的数据
【发布时间】:2017-09-24 19:16:17
【问题描述】:

我有一个 scd type 2 表,其中包含:-

ib  address start date  end date  flag
1   a        Jan-12     Dec-00    X
2   b        Jan-13     13-Mar    x
2   c        13-Mar     Dec-00    y
3   d        Feb-13     Dec-00    Y
1   v        Sep-17     Dec-00    Y

我想获取 2013 年所有员工的地址。 从表中选择 2013 年的所有活动记录。

【问题讨论】:

  • 如何识别员工?指定数据类型并发布您解决此问题的尝试。
  • start dateend date的数据类型是什么?

标签: sql reporting etl business-intelligence


【解决方案1】:

您要检索两组数据:

  • 员工地址从过去的任何时间有效,直到 2013 年或更晚(开始日期 = 2013)
  • 员工地址自 2013 年起生效。

这为您提供了 2013 年有效的地址的整个数据集。您可能有每个员工不止一个地址,因此您需要在您的选择声明中包含有效日期。

假设表名为addresses,并且日期列的格式正确:

select *
from addresses
where
    -- addresses valid until 2013 or later
    (year(start_date) < 2013 and year(end_date) >= 2013)
    -- addresses  which started being valid in 2013
    OR (year(start_date) = 2013)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    相关资源
    最近更新 更多