SQL summary of missing numbers in sequence

janez_007

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
missing numbers: 0, 1, 5, 6, 7, 10, 11
min number: 0 (always)
max number: max number of the sequence (15 in this example)

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). 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.

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 
Evaldas Buinauskas

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/

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find Missing multiple numbers sequence in SQL server

From Dev

SQL to find missing numbers in sequence starting from min?

From Dev

Given a sequence of numbers how to identify the missing numbers

From Dev

Find missing numbers with SQL

From Dev

Find first missing number in a sequence of numbers

From Dev

C program to find missing integer in a sequence of numbers

From Dev

Count the sequence of numbers while skipping missing values

From Dev

C program to find missing integer in a sequence of numbers

From Dev

difference between numbers in sequence when ids missing

From Dev

SQL Query for Select Sequence Numbers

From Dev

SQL to get sequence of phone numbers

From Dev

SQL Query for Select Sequence Numbers

From Dev

SQL Identity issue (missing numbers)

From Dev

Generating missing years for a sequence SQL server

From Dev

Finding missing sequence number in SQL 2008 table

From Dev

SQL query find missing consecutive numbers

From Dev

I have missing numbers in the data in sql

From Dev

how to find out missing numbers in sql table

From Dev

How to generate sequence numbers for hierarchical data in sql server

From Dev

Generate realistic sequence of numbers in SQL based on a random floor/ceiling

From Dev

Finding Missing Numbers When Data Is Grouped In SQL Server

From Dev

Finding Missing Numbers series when Data Is Grouped in sql server

From Dev

Finding the number missing in the sequence

From Dev

Round sequence of numbers to chosen numbers

From Dev

Generating sequence numbers in Java

From Dev

Numbers to letters with logical sequence

From Dev

Subsequence in a sequence of numbers

From Dev

Sequence of Fibonnaci Numbers - Prolog

From Dev

Camel File Sequence Numbers

Related Related

HotTag

Archive