如何用sql找出中間斷號的數字

paulyibinyi發表於2008-01-03

因為工作需要 需要找出id中間斷號的數字

create table t as select rownum id from dual connect by rownum<=20;

delete from t where id in (3,10,11,12);

commit;

SQL> with my_q as (
  2  select s_id, e_id
  3  from (select lag(max(id)) over(order by min(id))+1 s_id,min(id)-1 e_id
  4  from t
  5  group by id-rownum)
  6  where s_id is not null)
  7  select s_id + rn id
  8  from my_q,(select rownum-1 rn from dual connect by rownum<10)
  9  where s_id+rn <= e_id
 10  order by id
 11  /
 
        ID
----------
         3
        10
        11
        12

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7199859/viewspace-85989/,如需轉載,請註明出處,否則將追究法律責任。

相關文章