create table TimeGap(
Name char(4),
StartDate Date,
EndDate Date,
primary key (Name,StartDate));
insert into TimeGap values('John',to_date('20051101','yyyymmdd'),to_date('20051105','yyyymmdd'));
insert into TimeGap values('John',to_date('20051106','yyyymmdd'),to_date('20051109','yyyymmdd'));
insert into TimeGap values('John',to_date('20051112','yyyymmdd'),to_date('20051115','yyyymmdd'));
insert into TimeGap values('John',to_date('20051117','yyyymmdd'),to_date('20051117','yyyymmdd'));
insert into TimeGap values('Mike',to_date('20051119','yyyymmdd'),to_date('20051125','yyyymmdd'));
insert into TimeGap values('Mike',to_date('20051126','yyyymmdd'),to_date('20051130','yyyymmdd'));
insert into TimeGap values('Tom' ,to_date('20051110','yyyymmdd'),to_date('20051117','yyyymmdd'));
insert into TimeGap values('Tom' ,to_date('20051119','yyyymmdd'),to_date('20051125','yyyymmdd'));
commit;
--■■■Lead関数を使う方法■■■
select Name,GapStart,GapEnd from(
select Name,
EndDate+1 as GapStart,
Lead(StartDate) over(partition by Name order by StartDate) -1 as GapEnd
from TimeGap)
where GapStart <= GapEnd
order by Name,GapStart;
--■■■Lag関数を使う方法■■■
select Name,GapStart,GapEnd from(
select Name,
Lag(EndDate) over(partition by Name order by StartDate) +1 as GapStart,
StartDate-1 as GapEnd
from TimeGap)
where GapStart <= GapEnd
order by Name,GapStart;
--■■■相関サブクエリを使う方法1■■■
select Name,
EndDate+1 as GapStart,
(select min(b.StartDate) -1 from TimeGap b
where b.Name=a.Name
and b.StartDate > a.StartDate) as GapEnd
from TimeGap a
where (select min(b.StartDate) -1 from TimeGap b
where b.Name=a.Name
and b.StartDate > a.StartDate) >= EndDate+1
order by Name,GapStart;
--■■■相関サブクエリを使う方法2■■■
select Name,
(select max(b.EndDate) + 1 from TimeGap b
where b.Name=a.Name
and b.StartDate < a.StartDate) as GapStart,
StartDate-1 as GapEnd
from TimeGap a
where (select max(b.EndDate) + 1 from TimeGap b
where b.Name=a.Name
and b.StartDate < a.StartDate) <= StartDate-1
order by Name,GapStart;