在表中找到第一个未使用的数字

发布于 2025-01-31 04:18:21 字数 261 浏览 3 评论 0 原文

我有一个带有ID列作为Interger的表。我想使用函数和使用时,重复或循环获得此列的最小未使用值。 例如,在这种情况下,未使用的值为4

ID
​​ 1
2
3
5

I have a table with ID column as interger. I want to get the minimum unused value for this column using function and using WHILE, REPEAT or LOOP.
For example, in this case the unused value is 4

ID
1
2
3
5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

超可爱的懒熊 2025-02-07 04:18:21

对于在数据集中未使用的最小值,可以使用以下内容-Fiddle

select min(t.id)+1 as min_unused from 
(select id, lead(id) over (order by id)-id gap_id from test) t 
where t.gap_id>1;

For min unused in a data-set as given, following can be used -

select min(t.id)+1 as min_unused from 
(select id, lead(id) over (order by id)-id gap_id from test) t 
where t.gap_id>1;

Fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文