0

I am trying to add case # based on Customer ID and Start & End Dates. As long as there is no break in the date ranges for a customer, same case # will be applied. See sample data. Is there way accomplish this in Teradata SQL?

data sample

4

1 回答 1

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;
于 2018-12-12T23:29:54.140 回答