create table Tes1(
Code number(1),
Year char(6),
Kekka number(1));
insert into tes1 values(1,'200401',1);
insert into tes1 values(1,'200402',1);
insert into tes1 values(1,'200403',2);
insert into tes1 values(1,'200404',1);
insert into tes1 values(2,'200401',1);
insert into tes1 values(2,'200402',2);
insert into tes1 values(2,'200403',1);
insert into tes1 values(2,'200404',1);
insert into tes1 values(3,'200401',1);
insert into tes1 values(4,'200401',1);
insert into tes1 values(4,'200402',2);
insert into tes1 values(4,'200403',2);
insert into tes1 values(4,'200404',1);
insert into tes1 values(5,'200312',1);
insert into tes1 values(5,'200401',1);
insert into tes1 values(5,'200412',1);
commit;
--■■■existsを使う方法■■■
select Code,Year,Kekka
from Tes1 a
where not exists(select 1 from Tes1 b
where b.Code=a.Code
and b.Year=to_char(add_months(to_date(a.Year,'yyyymm'),1),'yyyymm')
and b.Kekka=1)
and a.Kekka=1
order by Code,Year;
--■■■分析関数を使う方法■■■
select Code,Year,Kekka
from (select Code,Year,
Lead(Year) over(partition by Code order by Year) as LeadYear,
Kekka,
Lead(Kekka) over(partition by Code order by Year) as LeadKekka
from Tes1)
where Kekka=1
and not (LeadKekka = 1
and LeadYear=to_char(add_months(to_date(Year,'yyyymm'),1),'yyyymm'))
or LeadYear is null;