你可以帮我解决这个问题吗?我的数据如下表所示
输入:
IDNO EFFECTIVE DATE TERM DATE
-------------------------------------
100 10/01/2001 12/01/2012
100 03/12/2013 05/02/2013
100 05/03/2013 01/04/2014
100 10/10/2014 12/31/9999
200 01/01/2017 02/15/2017
200 03/01/2017 12/31/2017
所需的输出:
IDNO EFFECTIVE DATE TERM DATE
-------------------------------------
100 10/01/2001 03/11/2012
100 03/12/2013 05/02/2013
100 05/03/2013 10/09/2014
100 10/10/2014 12/31/9999
200 01/01/2017 02/28/2017
200 03/01/2017 12/31/9999
学期日期应为生效日期前一天,最新记录期限日期应始终为12/31/9999
我可能有相同ID的N条记录。
在MM/DD/YY
答案 0 :(得分:1)
您可以使用lead()
:
with toupdate as (
select t.*,
lead(effective_date) over (partition by idno order by effective_date) as next_effective_date
from t
)
update toupdate
set term_date = coalesce(dateadd(day, -1, next_effective_date), '9999-12-31')
where term_date <> coalesce(dateadd(day, -1, next_effective_date), '9999-12-31');