Solved

Finding missing records in SQL table

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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