Prova den här också. Jag testade det så gott jag kunde, jag tror att det täcker alla möjligheter, inklusive sammansmältning av angränsande intervall (10:15 till 10:30 och 10:30 till 10:40 kombineras till ett enda intervall, 10:15 till 10:40 ). Den ska också vara ganska snabb, den använder inte mycket.
with m as
(
select ip_address, start_time,
max(stop_time) over (partition by ip_address order by start_time
rows between unbounded preceding and 1 preceding) as m_time
from ip_sessions
union all
select ip_address, NULL, max(stop_time) from ip_sessions group by ip_address
),
n as
(
select ip_address, start_time, m_time
from m
where start_time > m_time or start_time is null or m_time is null
),
f as
(
select ip_address, start_time,
lead(m_time) over (partition by ip_address order by start_time) as stop_time
from n
)
select * from f where start_time is not null
/