Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8460745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:41:33+00:00 2026-06-10T13:41:33+00:00

I’m trying to identify the gaps in a range of numbers (SQL Server). My

  • 0

I’m trying to identify the gaps in a range of numbers (SQL Server). My scenario is below…

ID   Start   End
1      1      4
2      1      6
3      2      4
4      8     10
5     13     14

Visual
-------------------------------
1-2-3-4
1-2-3-4-5-6
  2-3-4
           - -8-9-10
                    - - -13-14

The result of this could be something along the lines of:

Table
-------------------------------
ID   Start   End   Gap
4      8     10    -1
5     13     14    -2

Ultimately, I want to have the gap ranges, but I should be able to figure that out from above…

Missing
7
11-12

I came up with solutions that are either too slow or don’t account for the overlap in ranges (ex ID 2)

CREATE TABLE #Docs (
  [Rank] INT, --DENSE_RANK () OVER(ORDER BY BegProd)
  ControlNumber BIGINT,
  BegProd INT,
  EndProd  INT
)

SELECT
  T1.ControlNumber,
  T1.BegProd,
  T1.EndProd,
  MAX(T2.EndProd) AS [PreviousEndProd],
  [Gap] = T1.BegProd - MAX(T2.EndProd) - 1
FROM #Docs T1
INNER JOIN #Docs T2
  ON T1.[Rank] = T2.[Rank] + 1
  AND T1.EndProd > T2.EndProd
GROUP BY T1.ControlNumber, T1.BegProd, T1.EndProd
HAVING T1.BegProd - MAX(T2.EndProd) > 1

There is over 2 million rows in this table and a range span of 1 to 1 billion

EDIT
Fixed ‘Missing’ table.
The gap column indicates how much of a gap there is before that start number. (ex missing #7 is 1 number)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:41:35+00:00Added an answer on June 10, 2026 at 1:41 pm

    Try this:

    create table #docs(id int, start int, [end] int)
    insert #docs values(1,1,4),(2,1,6),(3,2,4),(4,8,10),(5,13,14)
    
    ;with a as
    (
    select start, dense_rank() over (order by start) rn
    from #docs t where not exists (select 1 from #docs where t.start > start and t.start < [end])
    group by start
    ), b as
    (
    select [end], dense_rank() over (order by [end]) rn
    from #docs t where not exists (select 1 from #docs where t.[end] > start and t.[end] < [end])
    group by [end]
    )
    select 
    case when a.[start]= b.[end]+2 then cast(a.start-1 as varchar(21)) 
    else cast(b.[end]+1 as varchar(10)) +'-' +  cast(a.start - 1 as varchar(10)) end missing
    from a join b on a.rn - 1 = b.rn
    and a.[start] <> b.[end] + 1
    

    Result:

    Missing
    7
    11-12
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.