Essayez celui-ci aussi. Je l'ai testé du mieux que j'ai pu, je pense qu'il couvre toutes les possibilités, y compris la fusion d'intervalles adjacents (10h15 à 10h30 et 10h30 à 10h40 sont combinés en un seul intervalle, 10h15 à 10h40 ). Il devrait également être assez rapide, il n'utilise pas beaucoup.
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
/