Има елегантно (и ефективно) решение с помощта на match_recognize
клауза (която изисква Oracle 12.1 или по-нова версия).
select po, startdate, enddate
from orders
match_recognize (
partition by po
order by startdate
measures first(startdate) as startdate, max(enddate) as enddate
pattern ( c* n )
define c as max(enddate) + 1 >= next(startdate)
);