Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Finding missing records in SQL table

Posted on 2014-09-04
5
90 Views
Last Modified: 2014-11-28
The table stored in mySQL database contains the ID column, which is a number between 2000000 and 4000000. The numbers are ordered and sequential (increment by 1). I need a SQL query which can loop through this range and find all missing records (the database should contain 2000000 records, but currently contains only 1800000).

Thank you, experts!
0
Comment
Question by:cyber-33
5 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40303733
Create a table of numbers, 2 million to 4 million.
create table numbers (N bigint);
then do a loop that inserts the numbers you need.

Next select the numbers that have no counterpart in your table.

Select N from numbers where not exists (Select ID from MyTable where ID = N);
0
 

Author Comment

by:cyber-33
ID: 40303742
I would need a script that populates the new table you are suggesting.

Also, there should be a simpler way finding the missing records by using a cursor and a single query.
0
 
LVL 25

Accepted Solution

by:
Tomas Helgi Johannsson earned 500 total points
ID: 40303886
Hi!

Try this sql to find the missing values (gaps) in your sequence.

SELECT a.id+1 AS start, MIN(b.id) - 1 AS end
    FROM mytable AS a, mytable AS b
    WHERE a.id < b.id
    GROUP BY a.id
    HAVING start < MIN(b.id)

Open in new window


Regards,
       Tomas Helgi
0
 

Author Comment

by:cyber-33
ID: 40304311
Tomas, I tried running the query but it never came back... Can you please explain the logic behind it?
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40305885
... there should be a simpler way finding the missing records
Really?  How often do you need to do this?  If it's more than once, there is something suspicious about the overall design.  My sense is that any solution would be the best solution.  And with only two million records it's not a large process.

Please post the CREATE TABLE statement and show us some of your test data.  Armed with that we can almost certainly give you a tested-and-working code sample.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql query to calculate avaerage 21 52
awk and Pythagoras? 5 19
SQL group by query to return records with highest value 6 24
sql server query 12 24
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question