## Finding Gaps in a Sequential Number Sequence

 Author Message Lashams SSC Veteran Group: General Forum Members Points: 274 Visits: 48 Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/slasham/findinggapsinasequentialnumbersequence.asp Mike Arney SSC Veteran Group: General Forum Members Points: 205 Visits: 109 If you just need to identify IF a list of numbers is in sequence, but not where the gaps are, I believe you can do it in a single pass of the table. (Warning -- to understand this algorithm, some high school math is required!)-- Mathematical way of identifying if a list of numbers is sequential-- Based on formula:-- M + (M+1) + (M+2) + ... + (M+n-1) = n*M + n*(n-1)/2-- Calling the left side S (for Sum), and rearranging yields:-- n2 + (2*M - 1)*n - 2*S = 0-- Then the quadratic formula produces:-- n = ( (1-2*M) +- sqrt( (2*M -1)2 + 8*S) )/2-- I believe it can be shown that a list of numbers is a sequence if-- and only if the value in the sqrt() above is a perfect square.-- The code below is based on that assumption---- Mike Arney, 4/3/2006set nocount oncreate table Numbers (id int not null)insert Numbers values (6)insert Numbers values (7)insert Numbers values (8)insert Numbers values (9)insert Numbers values (10)insert Numbers values (11)insert Numbers values (12)-- insert Numbers values (14) -- uncomment for testingdeclare @Sum bigint, @Count bigint, @Min bigintselect @Sum = sum(convert(bigint, id)), @Count = count(*), @Min = min(id) from Numbersdeclare @sqrt floatselect @sqrt = sqrt( power((2*@Min - 1), 2) + 8*@Sum)if @sqrt = floor(@sqrt) select 'Sequence' else select 'Not Sequence'godrop table Numbersgo Mike Gress SSC Rookie Group: General Forum Members Points: 35 Visits: 52 A self join solution:select a.SeqNumber, max(b.SeqNumber)from #SequenceTable a join #SequenceTable b on a.SeqNumber > b.SeqNumbergroup by a.SeqNumberhaving a.SeqNumber - max(b.SeqNumber) > 1 Brendon Smith-276513 Grasshopper Group: General Forum Members Points: 21 Visits: 7 This way is a few hundred times faster (on large datasets)...NOTE:the details are missing but it still identifies the key holes in the sequence, except beiging and end which are obvious...?SELECT s1.SeqNumberFROM #SequenceTable s1LEFT JOIN #SequenceTable s2ON s1.SeqNumber = s2.SeqNumber -1WHERE s2.SeqNumber IS NULL Scott Coleman SSCertifiable Group: General Forum Members Points: 7516 Visits: 1534 I agree, the join on "where Seq2.SeqNumber < Seq1.SeqNumber" will be a killer with large tables.This version uses a simple a = b-1 join to find the gaps, then a subquery to find the last value before the gap. On 1 million rows it ran in 1/4 the elapsed time and 1/13 the CPU time of the original in the article.select LastSeqNumber, NextSeqNumber , FirstAvailable = LastSeqNumber + 1 , LastAvailable = NextSeqNumber - 1 , NumbersAvailable = NextSeqNumber - (LastSeqNumber + 1) from ( select (SELECT TOP 1 SeqNumber FROM #SequenceTable WHERE SeqNumber < a.SeqNumber ORDER BY SeqNumber DESC) as LastSeqNumber, a.SeqNumber as NextSeqNumber from #SequenceTable a left join #SequenceTable b on a.SeqNumber = b.SeqNumber + 1 where b.SeqNumber IS NULL) aorder by LastSeqNumber Hans Lindgren SSCrazy Group: General Forum Members Points: 2940 Visits: 366 Nice solution!I would like to contribute too. If we only want to find IF there is a gap in the sequence we only need to verify that COUNT(*) + MIN(ID) = MAX(ID), right?In that case, if we have an index on the ID columns we only need a quick scan on it with very little additional CPU+Memory usage with:create table Numbers (id int not null)insert Numbers values (6)insert Numbers values (7)insert Numbers values (8)-- insert Numbers values (14) -- uncomment for testing-- DELETE Numbers WHERE id = 14SELECT CASE WHEN COUNT(*) + MIN(ID) - 1 = MAX(ID) THEN 'Sequence' ELSE 'Not Sequence' END FROM NUMBERSgodrop table Numbers Lashams SSC Veteran Group: General Forum Members Points: 274 Visits: 48 Thanks all for the great examples of other ways to get the same results. It is interesting to see these different methods and the understandings of how SQL works under the hood so to speak, with different timings on the various methods. For me timing was not a big issue, and my 31 second run over a table of 3.5 million rows seemed fair. Timing becomes more significant if a process is being repetatively and frequently run. Over my dataset, Scott Coleman’s method ran in 25 seconds, being significantly slower than his own reported difference, indicating other factors come into play. Brendan Smith’s method ran in 10 seconds, and although I accept this is faster, it does not do the computes for the additional rows in my output set, so is not a reliable comparison, but looks promising. Mike Gress’s example did not complete, I bombed it off at over 14 minutes with no results (sorry Mike). I didn’t try Hanslindgren’s method as it is not my database, so I was unable to add indexes to it. To Mike Arney, my high school maths was never that good, but I am sure there will be some that can work this one out from your example. Joe Celko’s second method also did not complete, and I bombed this off at over 14 minutes with no results, and Joe’s first method took less than a second and produced no results (sorry Joe). Looking deeper at my source dataset, the timing differences I get to your own results may be due to the extreme number of gaps. My data set produced 1.1 million rows (of identified gaps). Also within my data set are sequence number duplications, which may have contributed to the failure of some of the scripts. Thanks you all for your feedback and suggestions. Stephen Hans Lindgren SSCrazy Group: General Forum Members Points: 2940 Visits: 366 Hi Stephen!Thank you, it is not always you get such an exhausting feedback of how things turn out when you post replies and hope you can help.Hanslindgren

## Permissions

 You can't post new topics. You can't post topic replies. You can't post new polls. You can't post replies to polls. You can't edit your own topics. You can't delete your own topics. You can't edit other topics. You can't delete other topics. You can't edit your own posts. You can't edit other posts. You can't delete your own posts. You can't delete other posts. You can't post events. You can't edit your own events. You can't edit other events. You can't delete your own events. You can't delete other events. You can't send private messages. You can't send emails. You can read topics. You can't vote in polls. You can't upload attachments. You can download attachments. You can't post HTML code. You can't edit HTML code. You can't post IFCode. You can't post JavaScript. You can post emoticons. You can't post or upload images.

##### Select a forum
 SQL Server 2016      SQL Server 2016 - Administration      SQL Server 2016 - Development and T-SQL SQL Server 2014      Administration - SQL Server 2014      Development - SQL Server 2014 SQL Server 2012      SQL 2012 - General      SQL Server 2012 - T-SQL SQL Server vNext      SQL Server 14 - Administration      SQL Server 14 - Development SQL Server 2008      SQL Server 2008 - General      T-SQL (SS2K8)      June 2007 CTP      Working with Oracle      July CTP      SQL Server Newbies      Security (SS2K8)      SQL Server 2008 High Availability      SQL Server 2008 Administration      Data Corruption (SS2K8 / SS2K8 R2)      SQL Server 2008 Performance Tuning Cloud Computing      SQL Azure - Development      SQL Azure - Administration      Amazon AWS and other cloud vendors      General Cloud Computing Questions Reporting Services      Reporting Services      Reporting Services 2005 Administration      Reporting Services 2005 Development      Reporting Services 2008/R2 Administration      Reporting Services 2008 Development      SSRS 2012      SSRS 2014      SSRS 2016 Programming      Connecting      General      SMO/RMO/DMO      XML      Service Broker      Powershell      Testing      TFS/Data Dude/DBPro      SSDT      Continuous Integration, Deployment, and Delivery      R Services and R Language Data Warehousing      Integration Services      Strategies and Ideas      Analysis Services      Data Transformation Services (DTS)      Performance Point      Data Mining      PowerPivot      R language      Machine Learning Database Design      Disaster Recovery      Design Ideas and Questions      Relational Theory      Hardware      Virtualization SQLServerCentral.com      Anything that is NOT about SQL!      Contests!      Editorials      SQLServerCentral.com Announcements      SQLServerCentral.com Website Issues      Suggestions      Tag Issues with Content      Podcast Feedback      SQLServerCentral.com Test Forum      Articles Requested SQL Server 2005      Administering      Backups      Business Intelligence      CLR Integration and Programming.      Data Corruption      Development      Working with Oracle      SQL Server 2005 Compact Edition      SQL Server 2005 General Discussion      SQL Server 2005 Security      SQL Server 2005 Strategies      SS2K5 Replication      SQL Server Express      SQL Server 2005 Performance Tuning      SQL Server 2005 Integration Services      T-SQL (SS2K5)      SQL Server Newbies SQL Server 7,2000      Administration      Backups      Data Corruption      General      Globalization      In The Enterprise      Working with Oracle      Security      Strategies      SQL Server Newbies      Service Packs      SQL Server CE      Performance Tuning      Replication      Sarbanes-Oxley      T-SQL      SQL Server Agent SQL Server and other platforms      MySQL      Oracle      PostgreSQL      DB2      SQL Server and Sharepoint Older Versions of SQL (v6.5, v6.0, v4.2)      Older Versions of SQL (v6.5, v6.0, v4.2) Career      Certification      Employers and Employees      Events      Job Postings      Resumes and Job Hunters      Presentations and Speaking      Retired Members Testing Center      Question of the Day (QOD)      SQL Server Security Skills Microsoft Access      Microsoft Access Products and Books      Third Party Products         CA         Extreme Technologies.         Innovartis         Embarcadero         SQL Sentry         Sonasoft         SQLCentric         Golden Gate Software         Lumigent         Red Gate Software         Quest Software         ApexSQL         Idera      Discussions about Books         Discuss Programming Books          Discuss XML Books          Discuss T-SQL Books          Discuss Data Warehousing Books          Discuss DTS Books          Discuss SQL Server 7.0 Books         Discuss SQL Server 2000 Books Notification Services      Administration Article Discussions Future Versions      SQL 12