查询帮助需要查找缺失的号码
我有一个看起来像这样的表:
ID | GROUP
-------------------
1000001 | 0
1000001 | 1
1000001 | 2
1000002 | 0
1000002 | 2
1000002 | 3
1000003 | 1
1000003 | 2
1000003 | 3
1000004 | 0
我需要列出序列中缺少组的所有 id。 所以对于上面的例子我只需要返回 1000002 & 1000003.
提前致谢
I have a table that looks something like this:
ID | GROUP
-------------------
1000001 | 0
1000001 | 1
1000001 | 2
1000002 | 0
1000002 | 2
1000002 | 3
1000003 | 1
1000003 | 2
1000003 | 3
1000004 | 0
I need to list all the ids where there is a group missing in the sequence.
So for the above example i would only need back 1000002 & 1000003.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许是这样的:(
我用小写写了键)。如果您在
(ID, GROUP)
上有唯一键,则可以省略DISTINCT
。Maybe this:
(I've written the keys in lowercase). If you have a unique key on
(ID, GROUP)
, you can leave out theDISTINCT
.我从您的评论中得到的是,ID 必须以 0 组开头,并且该组应该始终仅递增 1。由此,我得到以下信息:
希望这会有所帮助。它可能不是最干净或最有效的,但希望它会有所帮助。
编辑:实际上,在重新查看我之前的答案并思考 HAVING 之后,它可能会像这样更干净。
What I'm getting from your comments is that the ID must start with a group of 0 and the group should always increment only by 1. From that, I get this:
Hope this helps. It's probably not the cleanest or most effective, but hope it will help.
EDIT: Actually, after re-looking over the answer before mine and thinking about HAVING, it probably would be cleaner like this.
由于我们不需要有关缺失组编号的信息,因此我们可以比较特定组的所有元素计数是否低于或等于
As we don need the information about the missing group number we can compare that over all count of elements is lower or equal for specific group