以下のテーブルから空き番号を取得する。
No
--
1
2
3
6
7
9
10
14
出力結果
空き
-----
4-5
8
11-13
create table NoTable2(No number(2)); insert into NoTable2 values( 1); insert into NoTable2 values( 2); insert into NoTable2 values( 3); insert into NoTable2 values( 6); insert into NoTable2 values( 7); insert into NoTable2 values( 9); insert into NoTable2 values(10); insert into NoTable2 values(14); commit;
col 空き for a8
select
LPad(case when No-LagNo=2 then to_char(No-1)
else to_char(LagNo+1) || '-' || to_char(No-1) end,5) as 空き
from (select No,
Lag(No) over(order by No) as LagNo
from NoTable2)
where No-LagNo!=1
order by No;
case式で、前のNoとの差に応じて、出力を分岐させて、 LPad関数で空白埋めしてます 前のNoとの差が2以上でないレコードは出力しないようにしてます。 7-28 空き番号を取得その1 7-30 空き番号を取得その3