我有下表
Createdt Updatedt id
1/1/2019 3/1/2019 1
1/1/2019 5/1/2019 1
1/1/2019 7/1/2019 1
预期结果:
Createdt Start_dt_cycle End_dt_cycle id
1/1/2019 1/1/2019 2/28/2019 1
1/1/2019 3/1/2019 4/30/2019 1
1/1/2019 5/1/2019 06/30/2019 1
1/1/2019 7/1/2019 12/31/9999 1
我的结果:
Createdt Start_dt_cycle End_dt_cycle id
1/1/2019 3/1/2019 4/30/2019 1
1/1/2019 5/1/2019 06/30/2019 1
1/1/2019 7/1/2019 12/31/9999 1
我使用了LEAD
函数来捕获所需的end_dt。我正在使用updatet创建周期,但我确实希望周期从createdt开始而不是第一次更新。
答案 0 :(得分:0)
您似乎想要union all
和lead()
:
select id, start_dt_cycle,
lead(start_dt_cycle, 1, date '9999-12-31') over (partition by id order by start_dt_cycle) as end_dt_cycle
from ((select min(createddt) as start_dt_cycle, id
from t
group by id
) union all
(select updatedt, id
from t
)
) t