管理日期范围 SQL 的历史记录

Managing historical records with date range SQL

我在 Teradata 中有一个 table,其中包含如下历史数据:

Table_A

A|B|C|  d_d       |   d_f
1|8|2|'2020-01-01'|'2020-02-01'
1|8|2|'2020-02-02'|'2020-03-31'
1|8|3|'2020-04-01'|'2020-05-11'
1|8|3|'2020-05-11'|'9999-12-31'
7|4|5|'2020-07-01'|'2020-09-12'
7|4|5|'2020-09-13'|'9999-12-31'

在输出中我正在寻找这样的东西:

    A|B|C|  d_d       |   d_f
    1|8|2|'2020-01-01'|'2020-03-31'
    1|8|3|'2020-04-01'|'9999-12-31'
    7|4|5|'2020-07-01'|'9999-12-31'

我试过了,但它遗漏了一些要忽略的行(例如示例中的第 2、4、6 行)

select 
A
,B
,C
,d_d
,case when lead(C)over(partition by a,b order by d_d) <> C 
then cast('9999-12-31' as date)
else lead(d_f)over(partition by a,b order by d_d) end as d_f

from table_a

对于您提供的数据,聚合应该有效:

select a, b, c, min(d_d), max(d_f)
from a
group by a, b, c;

Teradata 中有一个很好的 SQL 扩展来标准化重叠周期。它仅适用于数据类型 PERIOD,但可以即时创建:

with cte as
 (
   select NORMALIZE
      A
     ,B
     ,C
     -- PERIODs are inclusive-exclusive, the -1 adjusts for that
     ,period(d_d -1 , d_f) as pd 
   from table_a
 )
   select
      A
     ,B
     ,C
     ,begin(pd) +1 as d_d -- revert back to inclusive-inclusive
     ,end(pd) as d_f
   from cte