获取 Table 中的所有有效时间片

Get all Valid time slices in a Table

我有一个 table,其中的条目使用 SCD 2 进行了历史记录 现在我正在寻找获得 Alle PK 的可能性 同时有效:

例如我的table看起来像这样:

PK;ValidFrom;ValidTo
635582110901;2016-01-04;2016-01-21
635582110901;2016-01-22;2016-01-26
635582110901;2016-01-27;2016-02-14
635582110901;2016-02-15;2016-11-10
**635582110901;2016-11-11;2017-01-23**
**635582110901;2016-11-16;2016-12-12**
635582110901;2016-12-13;2017-01-18
635582110901;2017-01-19;2017-01-22
635582110901;2017-01-23;2017-01-23
635582110901;2017-01-24;2017-02-21
635582110901;2017-02-22;9999-12-31

select 应该给我两个粗体行

感谢您的帮助

您可以使用 exists:

select t.*
from t
where exists (select 1
              from t t2
              where t2.validfrom < t.validto and
                    t2.validto > t.validfrom and
                    t2.pk = t.pk and
                    t2.validfrom <> t.validfrom and
                    t2.validto <> t.validto
             );