Solved

Finding missing records in SQL table

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

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 108

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now