Solved

Finding missing records in SQL table

Posted on 2014-09-04
5
91 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 110

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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