यह एक प्रकार की गैप्स-एंड-आइलैंड समस्या है। आप lag()
. का उपयोग कर सकते हैं और फिर एक संचयी राशि:
select id, min(laufd), max(nextdt),
row_number() over (partition by id order by min(laufd)) as period
from (select t.*,
sum(case when prev_nextdt >= laufd - interval '2' day then 0 else 1 end) over
(partition by id order by order_row) as grp
from (select t.*,
lag(nextdt) over (partition by id order by order_row) as prev_nextdt
from t
) t
) t
group by grp, id;
संपादित करें:
यदि मान स्ट्रिंग के रूप में संग्रहीत हैं, तो उपयोग करें:
select id, min(laufd), max(nextdt),
row_number() over (partition by id order by min(laufd)) as period
from (select t.*,
sum(case when prev_nextdt >= laufd - interval '2' day then 0 else 1 end) over
(partition by id order by order_row) as grp
from (select t.id, t.order_row, -- any other columns you need
to_date(laufd, 'YYYYMMDD') as laufd,
to_date(nextdt, 'YYYYMMDD') as next_dt,
lag(to_date(nextdt, 'YYYYMMDD')) over (partition by id order by order_row) as prev_nextdt
from t
) t
) t
group by grp, id;