2016-07-16 51 views

回答

1

下面是一個方法:

select min(t.col) + 1 
from t 
where not exists (select 1 from t t2 where t2.col = t.col + 1); 

編輯:

如果我假設你想要的 「1」,如果它是從序列丟失,那麼:

select (case when max(tm.mincol) > 1 then 1 
      else min(t.col) + 1 
     end) as first_missing 
from t cross join 
    (select min(col) as mincol, max(col) as maxcol 
     from t 
    ) tm 
where not exists (select 1 from t t2 where t2.col = t.col + 1); 
+0

選擇(情況下,當最大(tm.REF)> 1,則1 否則分鐘(gbs_don.REF)+ 1 端)作爲first_missing 從gbs_don交叉聯接 (SELECT MIN(REF)作爲mincol ,max(REF)as maxcol from gbs_don )tm 其中不存在(從gbs_don t2中選擇1,其中t2.REF = t.REF + 1); –

+0

上面我的SQL查詢,給我一個錯誤:未知的列tm.REF –