asked on
select
clientid, startdate, enddate
from
(select
epi_client clientid,
(case
when o_plans.plan_client is not null and (EPI_START_DATE < PLAN_START_DATE) and (nvl(EPI_END_DATE, sysdate) between PLAN_START_DATE and nvl (PLAN_END_DATE, sysdate)) then EPI_START_DATE
when o_plans.plan_client is not null and (EPI_START_DATE <= PLAN_START_DATE) and (nvl(EPI_END_DATE, sysdate) >= nvl(PLAN_END_DATE, sysdate)) then EPI_START_DATE
when o_plans.plan_client is not null and (EPI_START_DATE between PLAN_START_DATE and nvl(PLAN_END_DATE, sysdate)) and (nvl(EPI_END_DATE, sysdate) > nvl(PLAN_END_DATE, sysdate)) then PLAN_START_DATE
when o_plans.plan_client is not null and (EPI_START_DATE > PLAN_START_DATE) and (nvl(EPI_END_DATE, sysdate) < nvl(PLAN_END_DATE, sysdate)) then PLAN_START_DATE
else EPI_START_DATE
end) as startdate,
(case
when o_plans.plan_client is not null and (EPI_START_DATE < PLAN_START_DATE) and (nvl(EPI_END_DATE, sysdate) between PLAN_START_DATE and nvl (PLAN_END_DATE, sysdate)) then PLAN_END_DATE
when o_plans.plan_client is not null and (EPI_START_DATE <= PLAN_START_DATE) and (nvl(EPI_END_DATE, sysdate) >= nvl(PLAN_END_DATE, sysdate)) then EPI_END_DATE
when o_plans.plan_client is not null and (EPI_START_DATE between PLAN_START_DATE and nvl(PLAN_END_DATE, sysdate)) and (nvl(EPI_END_DATE, sysdate) > nvl(PLAN_END_DATE, sysdate)) then EPI_END_DATE
when o_plans.plan_client is not null and (EPI_START_DATE > PLAN_START_DATE) and (nvl(EPI_END_DATE, sysdate) < nvl(PLAN_END_DATE, sysdate)) then PLAN_END_DATE
else EPI_END_DATE
end) as enddate
from
o_plans, o_episodes
where
o_plans.plan_client (+) = o_episodes.epi_client
union all
select plan_client clientid, plan_start_Date planstart, plan_end_Date planend
from o_plans
where not exists (select 1 from o_episodes
where epi_client = plan_client
and ((EPI_START_DATE < PLAN_START_DATE) and (nvl(EPI_END_DATE, sysdate) between PLAN_START_DATE and nvl (PLAN_END_DATE, sysdate))
or (EPI_START_DATE <= PLAN_START_DATE) and (nvl(EPI_END_DATE, sysdate) >= nvl(PLAN_END_DATE, sysdate))
or (EPI_START_DATE between PLAN_START_DATE and nvl(PLAN_END_DATE, sysdate)) and (nvl(EPI_END_DATE, sysdate) > nvl(PLAN_END_DATE, sysdate))
or (EPI_START_DATE > PLAN_START_DATE) and (nvl(EPI_END_DATE, sysdate) < nvl(PLAN_END_DATE, sysdate)))))
order by clientid, startdate
date-range-sample2.xls
ASKER
ASKER
plan: - - - - - a = = = = b - - - - -
episodes: - c = d - - - - - - - - - - - - -- too early
- - c = d - - - - - - - - - - - -- contiguous, extend
- - - - - - - c = d - - - - - - -- overlap, ignore
- - - - - - - - - - c = d - - - -- overlap, extend
- - - - - - - - - - - - c = d - -- too late
strict overlap: d >= a and c <= b
or contiguous: d >= a-1 and c <= b+1
extend: d >= a-1 and c < a or d > b and c <= b+1
(respectively extend left and extend right)
ASKER
select
PLAN_CLIENT as client,
nvl((
select min(EPI_START_DATE)
from o_episodes
where EPI_CLIENT=PLAN_CLIENT
and (EPI_START_DATE<PLAN_START_DATE)
and (EPI_END_DATE +1 >=PLAN_START_DATE or EPI_END_DATE is null)
), PLAN_START_DATE) as start_date,
nvl(nvl((
select max(nvl(EPI_END_DATE,sysdate))
from o_episodes
where EPI_CLIENT=PLAN_CLIENT
and (EPI_START_DATE<=PLAN_END_DATE +1 or PLAN_END_DATE is null)
and (EPI_END_DATE>PLAN_END_DATE or EPI_END_DATE is null)
), PLAN_END_DATE), sysdate) as end_date,
'p' as o
from o_plans
union all
select
EPI_CLIENT,
EPI_START_DATE,
nvl(EPI_END_DATE,sysdate),
'e' as o
from o_episodes
where not exists (
select 1 from o_plans
where PLAN_CLIENT=EPI_CLIENT
and (PLAN_START_DATE <=EPI_END_DATE +1 or EPI_END_DATE is null)
and (PLAN_END_DATE + 1 >=EPI_START_DATE or PLAN_END_DATE is null))
plans: - a = b - e = = f - - i = = j - note: three plans
episodes: - - - c = d - g = = h - k = l -
combined: - x = = = = = = = = = = = = y -
currently - a = = = d - - - - - - - - - - extended three plans
- - - c = = = = = = h - - - - -
- - - - - - - g = = = = = = j -
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
CREATE A VIEW ON EXISTING SQL SAY VIEW_X
and try this sql
select DISTINCT CLIENT_ID,STARTDATE,ENDDAT
MINUS
select a.CLIENT_ID,max(a.STARTDAT
VIEW_X a, VIEW_X b where a.pid=b.pid and a.STARTDATE>=b.STARTDATE and a.ENDDATE<=b.ENDDATE and a.rowid<>b.rowid
group by a.CLIENT_ID ;