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 168067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:23:15+00:00 2026-05-11T12:23:15+00:00

i have a table of IDs and positions CREATE TABLE #MissingSequence (ID INT NOT

  • 0

i have a table of IDs and positions

CREATE TABLE #MissingSequence (ID INT NOT NULL, Position INT NOT NULL) INSERT INTO #MissingSequence (ID,Position) SELECT 36,1 UNION ALL SELECT 36,2 UNION ALL SELECT 36,3 UNION ALL SELECT 36,4 UNION ALL SELECT 36,5 UNION ALL SELECT 36,6 UNION ALL SELECT 44,1 UNION ALL SELECT 44,3 UNION ALL SELECT 44,4 UNION ALL SELECT 44,5 UNION ALL SELECT 44,6 

What I am trying to find is if there is any break in the sequence of Positions by ID in this case the break between 44,1 and 44,3

I’ve managed to parse together:

SELECT  l.ID     ,Start_Position = MIN(l.Position) + 1     ,Stop_Position = MIN(fr.Position) - 1 FROM #MissingSequence l LEFT JOIN #MissingSequence r      ON l.Position = r.Position - 1 LEFT JOIN #MissingSequence fr      ON l.Position < fr.Position WHERE r.Position IS NULL     AND fr.Position IS NOT NULL GROUP BY l.ID 

but it doesn’t work if there are multiple ID values. It does work if only a single ID, 44 exists.

thoughts, comments, suggestions?

thanks!

  • 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. 2026-05-11T12:23:15+00:00Added an answer on May 11, 2026 at 12:23 pm

    The left self-join was a good instinct, but I don’t think the aggregates are going to cut it, and certainly you’d need to include the matching-ID clause in your self-joins.

    Here’s an (ANSI-compliant) version using the null-left-join idea, selecting a top row and a bottom row and checking there’s nothing between them:

    SELECT     above.ID AS ID, below.Position+1 AS Start_Position, above.Position-1 AS End_Position FROM MissingSequence AS above JOIN MissingSequence AS below     ON below.ID=above.ID AND below.Position<above.Position-1 LEFT JOIN MissingSequence AS inbetween     ON inbetween.ID=below.ID AND inbetween.Position BETWEEN below.Position+1 AND above.Position-1 WHERE inbetween.ID IS NULL;  +----+----------------+--------------+ | ID | Start_Position | End_Position | +----+----------------+--------------+ | 44 |              2 |            2 |  +----+----------------+--------------+ 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Add more data. By default MySQL will ignore any word… May 12, 2026 at 1:11 am
  • Editorial Team
    Editorial Team added an answer No - the strategy is defined at the event level.… May 12, 2026 at 1:11 am
  • Editorial Team
    Editorial Team added an answer The foreign key defines a parent - child relationship between… May 12, 2026 at 1:11 am

Related Questions

I have a table of groups, of people, and a many-to-many table of group_id
I have three tables in the many-to-many format. I.e, table A, B, and AB
HI I have a table with some some values (IDs), and of course when
i have a table called category in which i have main category ids and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.