SQL查询查找丢失的序列号
我有一个名为 sequence
的列。 此列中的数据看起来像 1, 2, 3, 4, 5, 7, 9, 10, 15。
我需要从表中找到丢失的序列号。 什么 SQL 查询可以从我的表中找到丢失的序列号? 我期待的结果就像
Missing numbers
---------------
6
8
11
12
13
14
我只使用一张表一样。 我尝试了下面的查询,但没有得到我想要的结果。
select de.sequence + 1 as sequence from dataentry as de
left outer join dataentry as de1 on de.sequence + 1 = de1.sequence
where de1.sequence is null order by sequence asc;
I have a column named sequence
. The data in this column looks like 1, 2, 3, 4, 5, 7, 9, 10, 15.
I need to find the missing sequence numbers from the table. What SQL query will find the missing sequence numbers from my table? I am expecting results like
Missing numbers
---------------
6
8
11
12
13
14
I am using only one table. I tried the query below, but am not getting the results I want.
select de.sequence + 1 as sequence from dataentry as de
left outer join dataentry as de1 on de.sequence + 1 = de1.sequence
where de1.sequence is null order by sequence asc;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
为我的公司开发客户编号生成器。 不是最有效的,但绝对是最可读的
该表有一个 Id 列。
该表允许用户手动插入 ID 序列。
该解决方案解决了用户决定选择较大数字的情况
Working on a customer number generator for my company. Not the most efficient but definitely most readable
The table has one Id column.
The table allows for Ids to be inserted at manually by a user off sequence.
The solution solves the case where the user decided to pick a high number
怎么样:
给出总结结果:
How about something like:
giving summarised results:
我知道这是一篇非常旧的帖子,但我想添加我发现的这个解决方案 这里这样我可以更容易地找到它:
I know this is a very old post but I wanted to add this solution that I found HERE so that I can find it easier:
尝试用这个:
Try with this:
最好的解决方案是使用带有序列的临时表。 假设您构建这样一个表,带有 NULL 检查的 LEFT JOIN 应该可以完成这项工作:
但是如果您必须经常重复此操作(并且对于数据库中的 1 个序列更多),我将创建一个“静态数据”表并具有将其填充到您需要的所有表的 MAX(值)的脚本。
The best solutions are those that use a temporary table with the sequence. Assuming you build such a table, LEFT JOIN with NULL check should do the job:
But if you have to repeat this operation often (and more then for 1 sequence in the database), I would create a "static-data" table and have a script to populate it to the MAX(value) of all the tables you need.
下面是一个用于创建存储过程的脚本,该存储过程返回给定日期范围内缺失的序列号。
去
Here is a script to create a stored procedure that returns missing sequential numbers for a given date range.
GO
所有给出的解决方案都太复杂了吗?
这不是更简单吗:
Aren't all given solutions way too complex?
wouldn't this be much simpler:
这是我对此问题的解释,将内容放置在一个表变量中,我可以在脚本的其余部分轻松访问该变量。
This is my interpretation of this issue, placing the contents in a Table variable that I can easily access in the remainder of my script.
只是为了好玩,我决定发布我的解决方案。
我的表中有一个身份列,我想找到丢失的发票号码。
我回顾了我能找到的所有示例,但它们不够优雅。
Just for fun, I decided to post my solution.
I had an identity column in my table and I wanted to find missing invoice numbers.
I reviewed all the examples I could find but they were not elegant enough.
我在这里找到了这个答案:
http://sql-developers.blogspot.com /2012/10/how-to-find-missing-identitysequence.html
我一直在寻找解决方案并找到了很多答案。 这是我用过的,效果非常好。 我希望这可以帮助任何寻找类似答案的人。
I found this answer here:
http://sql-developers.blogspot.com/2012/10/how-to-find-missing-identitysequence.html
I was looking for a solution and found many answers. This is the one I used and it worked very well. I hope this helps anyone looking for a similar answer.
创建一个有用的Tally 表:
对其进行索引,或将该单列设为 PK。
然后使用 EXCEPT 获取丢失的号码。
Create a useful Tally table:
Index it, or make that single column as PK.
Then use EXCEPT to get your missing number.
您还可以使用 CTE 之类的东西来生成完整序列:
嗯 - 由于某种原因,格式在这里不起作用? 任何人都可以看到问题吗?
You could also solve using something like a CTE to generate the full sequence:
Hmmmm - the formatting is not working on here for some reason? Can anyone see the problem?
我做了一个过程,这样你就可以发送表名和密钥,结果是给定表中缺失数字的列表
i had made a proc so you can send the table name and the key and the result is a list of missing numbers from the given table
此查询生成一个范围从 1 到 15 的序列。(根据您所需的范围进行更改)
通过选择 cte 表达式中没有的数字,您将获得缺失的数字。
This query generates a sequence that ranges from 1 to 15. (Change according to your desired range)
By selecting the numbers not in the cte expression you would get the missing numbers.