Есть набор временных промежутков, в том числе пересекающихся. Вывести их объединение. Без пересечений.
На Oracle
with src as (
select to_timestamp('2016-10-26 08:10:10', 'YYYY-MM-DD HH24:MI:SS') as t, +1 as v from dual union all
select to_timestamp('2016-10-26 08:20:10', 'YYYY-MM-DD HH24:MI:SS') , -1 as v from dual union all
select to_timestamp('2016-10-26 09:10:10', 'YYYY-MM-DD HH24:MI:SS') as t, +1 as v from dual union all
select to_timestamp('2016-10-26 09:20:10', 'YYYY-MM-DD HH24:MI:SS') , -1 as v from dual union all
select to_timestamp('2016-10-26 09:20:10', 'YYYY-MM-DD HH24:MI:SS') as t, +1 as v from dual union all
select to_timestamp('2016-10-26 09:35:10', 'YYYY-MM-DD HH24:MI:SS') , -1 as v from dual union all
select to_timestamp('2016-10-26 09:15:10', 'YYYY-MM-DD HH24:MI:SS') as t, +1 as v from dual union all
select to_timestamp('2016-10-26 09:40:10', 'YYYY-MM-DD HH24:MI:SS') , -1 as v from dual union all
select to_timestamp('2016-10-26 09:25:10', 'YYYY-MM-DD HH24:MI:SS') as t, +1 as v from dual union all
select to_timestamp('2016-10-26 09:45:10', 'YYYY-MM-DD HH24:MI:SS') , -1 as v from dual union all
select to_timestamp('2016-10-26 10:10:10', 'YYYY-MM-DD HH24:MI:SS') as t, +1 as v from dual union all
select to_timestamp('2016-10-26 10:20:10', 'YYYY-MM-DD HH24:MI:SS') , -1 as v from dual union all
select to_timestamp('2016-10-26 11:10:10', 'YYYY-MM-DD HH24:MI:SS') as t, +1 as v from dual union all
select to_timestamp('2016-10-26 11:20:10', 'YYYY-MM-DD HH24:MI:SS') , -1 as v from dual union all
select to_timestamp('2016-10-26 12:10:10', 'YYYY-MM-DD HH24:MI:SS') as t, +1 as v from dual union all
select to_timestamp('2016-10-26 12:20:10', 'YYYY-MM-DD HH24:MI:SS') , -1 as v from dual union all
select to_timestamp('2016-10-26 12:35:10', 'YYYY-MM-DD HH24:MI:SS') as t, +1 as v from dual union all
select to_timestamp('2016-10-26 12:45:10', 'YYYY-MM-DD HH24:MI:SS') , -1 as v from dual union all
select to_timestamp('2016-10-26 14:10:10', 'YYYY-MM-DD HH24:MI:SS') as t, +1 as v from dual union all
select to_timestamp('2016-10-26 14:20:10', 'YYYY-MM-DD HH24:MI:SS') , -1 as v from dual
)
select case vt when 0 then gr - 1 else gr end as gr, min(t) as b, max(t) as e
from (
select a.*, sum(case when vt = 0 then 1 else 0 end) over (order by t) as gr
from (
select s.*, sum(v) over (order by t) as vt
from src s
) a
) b
group by case vt when 0 then gr - 1 else gr end
order by 2