SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2004
    Location
    united states
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    finding a missing data in a table

    Hello

    I have a table which a column holding UserIds of type bigint(5)

    Usually I will have consecutive UserIds in the table, i.e.: 1,2, 3,4,...
    However, it is possible that the table will have whole holes in the data: 1,2,5,6, 8,...
    Is it possible to detect such holes using SQL?
    for example, if the data is: 1,2,5,6,8
    then I would like to get the lowest missing id. In the above example it would be 3

    thanks in advance

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    yes it is possible to find gaps in sequences

    but i will not show you the sql (it's rather complex) until you assure me that you will not attempt to re-use these numbers
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2004
    Location
    united states
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is the code I am using:

    SELECT (tab1.ID) missing FROM table tab1 LEFT JOIN table tab2 ON tab1.ID + 1 = tab2.ID WHERE tab2.ID IS NULL ORDER BY missing ASC LIMIT 1;

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    cool
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •