Поспешил, дубли остаются:
with
"_SCHEDULE" as (
select 0 as SCHEDULE_ID, '' as NAME from dual where 0=1
union all select 1, 'schedule1' from dual
union all select 2, 'schedule2' from dual
union all select 3, 'schedule3' from dual
),
"_SEGMENT" as (
select 0 as SEGMENT_ID, 0 as SCHEDULE_ID, '' as NAME, 0 as IS_SYSTEM from dual where 0=1
union all select 10, 1, 'segment1', 1 from dual
union all select 20, 2, 'segment2', 1 from dual
union all select 21, 2, 'segment2a', 0 from dual
union all select 22, 2, 'segment2b', 0 from dual
union all select 23, 2, 'segment2c', 0 from dual
union all select 24, 2, 'segment2d', 0 from dual
union all select 30, 3, 'segment3', 1 from dual
union all select 31, 3, 'segment3a', 0 from dual
union all select 32, 3, 'segment3b', 0 from dual
union all select 33, 3, 'segment3c', 0 from dual
),
"_EXTENT" as (
select 0 as EXTENT_ID, 0 as SEGMENT_ID, 0 as IS_SYSTEM, 0 as BEG_HOUR, 0 as END_HOUR from dual where 0=1
union all select 100, 10, 1, 00, 23 from dual
union all select 200, 20, 1, 00, 23 from dual
union all select 211, 21, 0, 23, 00 from dual
union all select 212, 21, 0, 00, 05 from dual
union all select 221, 22, 0, 06, 10 from dual
union all select 231, 23, 0, 11, 17 from dual
union all select 242, 24, 0, 19, 22 from dual
union all select 300, 30, 1, 00, 23 from dual
union all select 310, 31, 0, 00, 04 from dual
union all select 320, 32, 0, 06, 10 from dual
union all select 330, 33, 0, 12, 20 from dual
),
"_HOURS" as (select rownum-1 as HOUR from dual connect by level <= 24)
select * --P.SCHEDULE_ID, P.NAME as SCHEDULE, H.HOUR, nvl(S0.SEGMENT_ID,S1.SEGMENT_ID) as SEGMENT_ID, nvl(S0.NAME,S1.NAME) as SEGMENT, nvl(S0.IS_SYSTEM,S1.IS_SYSTEM) as IS_SYSTEM
from "_HOURS" H
join "_EXTENT" E1 on (H.HOUR between E1.BEG_HOUR and E1.END_HOUR and E1.IS_SYSTEM = 1)
left join "_EXTENT" E0 on (H.HOUR between E0.BEG_HOUR and E0.END_HOUR and E0.IS_SYSTEM = 0)
left join "_SEGMENT" S1 on (S1.SEGMENT_ID = E1.SEGMENT_ID)
left join "_SEGMENT" S0 on (S0.SEGMENT_ID = E0.SEGMENT_ID and S0.SCHEDULE_ID = S1.SCHEDULE_ID)
join "_SCHEDULE" P on (P.SCHEDULE_ID = nvl(S0.SCHEDULE_ID,S1.SCHEDULE_ID))
--where P.SCHEDULE_ID = 2 and H.HOUR = 19
order by P.SCHEDULE_ID, H.HOUR
Если в альясах E1 и E0 вместо таблицы использовать подзапрос (чтобы получить связанный SCHEDULE_ID), то проблема решается, но запрос становится уже громоздким.
Может быть можно задачу решить как-то иначе?