asked on
This requires splitting up the problem into its components. (are we no longer merging when it's the day before the next range?)
First, where there is an overlap, return the earliest start and the latest end.
select e.epi_client,
min(least(e.epi_start_date,p.plan_start_date)) startdate,
case when max(greatest(nvl(e.epi_end_date,add_months(trunc(sysdate),120))),
nvl(p.plan_end_date,add_months(trunc(sysdate),120))) =
add_months(trunc(sysdate),120)) then
'-'
else
to_char(max(greatest(nvl(e.epi_end_date,add_months(trunc(sysdate),120))),
nvl(p.plan_end_date,add_months(trunc(sysdate),120))),'DD-MON-YYYY') end enddate
from o_episodes e, o_plan p
where e.epi_client = p.plan_client and
(
e.epi_start_date between p.plan_start_date and nvl(p.plan_end_date,add_months(trunc(sysdate),120))
or
e.epi_end_date between p.plan_start_date and nvl(p.plan_end_date,add_months(trunc(sysdate),120))
or
(e.epi_end_date is null and e.epi_start_date < p.plan_end_date)
or
p.plan_start_date between e.epi_start_date and nvl(e.epi_end_date,add_months(trunc(sysdate),120))
or
p.plan_end_date between e.epi_start_date and nvl(e.epi_end_date,add_months(trunc(sysdate),120))
)
--
-- Now UNION ALL the non-overlapping
--
union all
select epi_client, epi_start_Date, nvl(to_char(epi_end_Date,'DD-MON-YYYY'), '-')
from o_episodes e
where not exists (select 1 from o_plan p
where epi_client = p.plan_client and
(
e.epi_start_date between p.plan_start_date and nvl(p.plan_end_date,add_months(trunc(sysdate),120))
or
e.epi_end_date between p.plan_start_date and nvl(p.plan_end_date,add_months(trunc(sysdate),120))
or
(e.epi_end_date is null and e.epi_start_date < p.plan_end_date)
or
p.plan_start_date between e.epi_start_date and nvl(e.epi_end_date,add_months(trunc(sysdate),120))
or
p.plan_end_date between e.epi_start_date and nvl(e.epi_end_date,add_months(trunc(sysdate),120))
))
union all
select plan_client, plan_start_Date, nvl(to_char(plan_end_Date,'DD-MON-YYYY'), '-')
from o_plan p
(
e.epi_start_date between p.plan_start_date and nvl(p.plan_end_date,add_months(trunc(sysdate),120))
or
e.epi_end_date between p.plan_start_date and nvl(p.plan_end_date,add_months(trunc(sysdate),120))
or
(e.epi_end_date is null and e.epi_start_date < p.plan_end_date)
or
p.plan_start_date between e.epi_start_date and nvl(e.epi_end_date,add_months(trunc(sysdate),120))
or
p.plan_end_date between e.epi_start_date and nvl(e.epi_end_date,add_months(trunc(sysdate),120))
)
)
Good luck!
ASKER
select e.epi_client,
min(least(e.epi_start_date,p.plan_start_date)) startdate,
case when max(greatest(nvl(e.epi_end_date,add_months(trunc(sysdate),120))),
nvl(p.plan_end_date,add_months(trunc(sysdate),120))) =
add_months(trunc(sysdate),120)) then
'-'
else
to_char(max(greatest(nvl(e.epi_end_date,add_months(trunc(sysdate),120))),
nvl(p.plan_end_date,add_months(trunc(sysdate),120))),'DD-MON-YYYY') end enddate
from o_episodes e, o_plan p
where e.epi_client = p.plan_client and
(
e.epi_start_date between p.plan_start_date and nvl(p.plan_end_date,add_months(trunc(sysdate),120))
or
e.epi_end_date between p.plan_start_date and nvl(p.plan_end_date,add_months(trunc(sysdate),120))
or p.plan_start_date - e.epi_end_date between 0 and 1 or e.epi_start_date - p.plan_end_date between 0 and 1
or
(e.epi_end_date is null and e.epi_start_date < p.plan_end_date)
or
p.plan_start_date between e.epi_start_date and nvl(e.epi_end_date,add_months(trunc(sysdate),120))
or
p.plan_end_date between e.epi_start_date and nvl(e.epi_end_date,add_months(trunc(sysdate),120))
)
--
-- Now UNION ALL the non-overlapping
--
union all
select epi_client, epi_start_Date, nvl(to_char(epi_end_Date,'DD-MON-YYYY'), '-')
from o_episodes e
where not exists (select 1 from o_plan p
where epi_client = p.plan_client and
(
e.epi_start_date between p.plan_start_date and nvl(p.plan_end_date,add_months(trunc(sysdate),120))
or
e.epi_end_date between p.plan_start_date and nvl(p.plan_end_date,add_months(trunc(sysdate),120))
or p.plan_start_date - e.epi_end_date between 0 and 1 or e.epi_start_date - p.plan_end_date between 0 and 1
or
(e.epi_end_date is null and e.epi_start_date < p.plan_end_date)
or
p.plan_start_date between e.epi_start_date and nvl(e.epi_end_date,add_months(trunc(sysdate),120))
or
p.plan_end_date between e.epi_start_date and nvl(e.epi_end_date,add_months(trunc(sysdate),120))
))
union all
select plan_client, plan_start_Date, nvl(to_char(plan_end_Date,'DD-MON-YYYY'), '-')
from o_plan p
(
e.epi_start_date between p.plan_start_date and nvl(p.plan_end_date,add_months(trunc(sysdate),120))
or
e.epi_end_date between p.plan_start_date and nvl(p.plan_end_date,add_months(trunc(sysdate),120))
or p.plan_start_date - e.epi_end_date between 0 and 1 or e.epi_start_date - p.plan_end_date between 0 and 1
or
(e.epi_end_date is null and e.epi_start_date < p.plan_end_date)
or
p.plan_start_date between e.epi_start_date and nvl(e.epi_end_date,add_months(trunc(sysdate),120))
or
p.plan_end_date between e.epi_start_date and nvl(e.epi_end_date,add_months(trunc(sysdate),120))
)
)
Good luck!
ASKER
select e.epi_client,
min(least(e.epi_start_date,p.plan_start_date)) startdate,
case when max(greatest(nvl(e.epi_end_date,add_months(trunc(sysdate),120)),
nvl(p.plan_end_date,add_months(trunc(sysdate),120)))) =
add_months(trunc(sysdate),120)) then
'-'
else
to_char(max(greatest(nvl(e.epi_end_date,add_months(trunc(sysdate),120)),
nvl(p.plan_end_date,add_months(trunc(sysdate),120)))),'DD-MON-YYYY') end enddate
from o_episodes e, o_plan p
ASKER
ASKER
Oracle is an object-relational database management system. It supports a large number of languages and application development frameworks. Its primary languages are SQL, PL/SQL and Java, but it also includes support for C and C++. Oracle also has its own enterprise modules and application server software.
TRUSTED BY
apart from that, this should be a good start:
Open in new window