【发布时间】:2016-07-16 09:22:12
【问题描述】:
我在 [1,2,3,4,6,7,9,11,12] 列中有以下整数值,我想编写一个查询,给出在这种情况下应该是 5 的最小正缺失整数
谢谢
【问题讨论】:
我在 [1,2,3,4,6,7,9,11,12] 列中有以下整数值,我想编写一个查询,给出在这种情况下应该是 5 的最小正缺失整数
谢谢
【问题讨论】:
这是一种方法:
select min(t.col) + 1
from t
where not exists (select 1 from t t2 where t2.col = t.col + 1);
编辑:
如果我假设如果序列中缺少“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);
【讨论】: