Prueba este también. Lo probé lo mejor que pude, creo que cubre todas las posibilidades, incluida la fusión de intervalos adyacentes (10:15 a 10:30 y 10:30 a 10:40 se combinan en un solo intervalo, 10:15 a 10:40 ). También debería ser bastante rápido, no consume mucho.
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
/