使用日期范围SQL管理历史记录



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

表_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

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

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

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

最新更新