简体   繁体   English

SQL按顺序缺少数字的摘要

[英]SQL summary of missing numbers in sequence

I would like to find gaps in a sequence and summarize the findings in the following way: 我想按顺序查找差距,并通过以下方式总结发现:

number sequence: 2, 3, 4, 8, 9, 12, 13, 14, 15 数字顺序: 2, 3, 4, 8, 9, 12, 13, 14, 15
missing numbers: 0, 1, 5, 6, 7, 10, 11 缺少数字: 0, 1, 5, 6, 7, 10, 11
min number: 0 (always) 最小人数:0(总是)
max number: max number of the sequence (15 in this example) max number:序列的最大数目(在此示例中为15)

The summary should look like: 摘要应如下所示:

From | To | # of missing  
00   | 01 | 2  
05   | 07 | 3  
10   | 11 | 2

I am using SQL server and in reality, the sequence will contain many more numbers (close to a million). 我正在使用SQL Server,实际上,该序列将包含更多数字(接近一百万)。 I have found many scripts that find and list the missing numbers in the sequence, but I can't figure out how to summarize it in the desired way. 我已经找到了许多脚本,这些脚本可以找到并列出序列中的缺失数字,但是我无法弄清楚如何以所需的方式对其进行汇总。

If it helps, the field is called BELNR and the table is called BSEG . 如果有帮助,则该字段称为BELNR ,而表称为BSEG

EDIT: with the help from the Gaps and Islands material, I have been able to find a solution (may not be the most optimal one, but I think it works): 编辑:在空白和离岛资料的帮助下,我已经找到了一种解决方案(可能不是最佳解决方案,但我认为它可行):

with C as
(
select belnr, row_number() over(order by belnr) as rownum
from bseg
)
select cast(Cur.belnr as bigint) + 1 as [From], cast(nxt.belnr as bigint) - 1 as [To], (cast(nxt.belnr as bigint) - 1) - (cast(Cur.belnr as bigint) + 1) + 1  as [# of Missing]
from C as Cur
join C as Nxt
    on Nxt.rownum = cast(Cur.rownum as int) +1
Where cast(nxt.belnr as bigint) - cast(Cur.belnr as bigint) > 1 

This is called Islands and Gaps problem. 这称为离岛和缺口问题。 Read more here: 在这里阅读更多:

https://www.simple-talk.com/sql/t-sql-programming/the-sql-of-gaps-and-islands-in-sequences/ https://www.simple-talk.com/sql/t-sql-programming/the-sql-of-gaps-and-islands-in-sequences/

The word 'Gaps' in the title refers to gaps in sequences of values. 标题中的“间隙”一词是指值序列中的间隙。 Islands are unbroken sequences delimited by gaps. 岛是不间断的序列,由空格分隔。 The 'Gaps and Islands' problem is that of using SQL to rapidly detect the unbroken sequences, and the extent of the gaps between them in a column. “空白和孤岛”问题是使用SQL快速检测未中断序列及其在列中的缺口程度的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM