连续日期以创建案例#

时间:2018-12-12 22:45:33

标签: sql teradata teradata-sql-assistant

我正在尝试根据客户ID和开始和结束日期添加案例编号。只要客户的日期范围没有中断,将应用相同的案例编号。查看样本数据。有没有办法在Teradata SQL中完成此任务?

data sample

1 个答案:

答案 0 :(得分:1)

您可以使用lag()和一个累积的总和来获得一个数字:

select t.*,
       sum(case when enddate = prev_enddate + interval '1' day
                then 0 else 1
           end) over (partition by customerid order by startdate
                     ) as result
from (select t.*,
             lag(enddate) over (partition by customerid order by startdate) as prev_enddate
      from t
     ) t;