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?

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?

您可以使用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;