def inID = 2
--■■■相関サブクエリを使う方法1■■■
select ID,name
from hogeTable
where ID in((select max(ID) from hogeTable where ID < &inID),
&inID,
(select min(ID) from hogeTable where ID > &inID))
order by ID;
--■■■相関サブクエリを使う方法2■■■
select ID,name
from hogeTable
where ID >= nvl((select max(ID) from hogeTable where ID < &inID),ID)
and ID <= nvl((select min(ID) from hogeTable where ID > &inID),ID)
order by ID;
--■■■Row_Number関数を使う方法■■■
select ID,Name
from (select ID,name,Row_Number() over(order by ID) as Rank,
sum(case when &inID >= ID then 1 else 0 end) over() as inIDRank
from hogeTable)
where abs(inIDRank-Rank) <= 1;
--■■■Lag関数とLead関数を使う方法(CountStopなし)■■■
select ID,name
from (select ID,name,
Lag(id) over(order by id) as LagID,
Lead(id) over(order by id) as LeadID
from hogeTable)
where &inID in (ID,LagID,LeadID);
--■■■Lag関数とLead関数を使う方法(CountStopあり)■■■
select ID,name
from (select ID,name,
Lag(id) over(order by id) as LagID,
Lead(id) over(order by id) as LeadID
from hogeTable)
where &inID in (ID,LagID,LeadID)
and RowNum <= 3;