with recs as (select * from
(
select 'prod2' pr, to_date('01.01.2015', 'dd.mm.yyyy') dt from dual union all
select 'prod2' pr, to_date('01.04.2015', 'dd.mm.yyyy') dt from dual union all
select 'prod2' pr, to_date('01.05.2015', 'dd.mm.yyyy') dt from dual union all
select 'prod2' pr, to_date('01.06.2015', 'dd.mm.yyyy') dt from dual union all
select 'prod2' pr, to_date('01.07.2015', 'dd.mm.yyyy') dt from dual union all
select 'prod2' pr, to_date('01.09.2015', 'dd.mm.yyyy') dt from dual union all
select 'prod2' pr, to_date('01.10.2015', 'dd.mm.yyyy') dt from dual union all
select 'prod2' pr, to_date('01.11.2015', 'dd.mm.yyyy') dt from dual union all
select 'prod2' pr, to_date('01.12.2015', 'dd.mm.yyyy') dt from dual union all
select 'prod1' pr, to_date('01.10.2015', 'dd.mm.yyyy') dt from dual union all
select 'prod1' pr, to_date('01.12.2015', 'dd.mm.yyyy') dt from dual
))
select
pr,
min(dt) dt,
max(dt1) dt1
from (
select
lv,
lf,
rt,
pr,
dt,
case when lf=1 and dl>1 then dt else dt1 end dt1,
dl
from (
select
level lv,
connect_by_isleaf lf,
connect_by_root dt rt,
t.*
from (
select
pr,
dt,
dt1,
months_between(dt1, dt) dl
from (
select
pr,
dt,
(select min(r1.dt) from recs r1 where r1.pr=r.pr and r1.dt>r.dt) dt1
from
recs r
)
) t
connect by
prior dt=dt1
and dl=1
start with dl is null or dl>1
)
where
not (lf=0 and dl>1)
)
group by
pr,
rt
order by
2
;
|