Solved

Find the missing number

Posted on 2011-03-16
7
843 Views
Last Modified: 2012-05-11
Being unexperienced in SQL, I have problem with this query:

This is my database table ("table_number"):

id      Number      Name                        
25      1      James
57      3      Doris
78      9      Dave
89      7      Chuck
126      2      Bertie
153      10      Marion
198      5      Veronica
234      6      Allen

If I order it by "Number" it will print:

id      Number      Name
                        
25      1      James
126      2      Bertie
57      3      Doris
198      5      Veronica
234      6      Allen
89      7      Chuck
78      9      Dave
153      10      Marion

Looking at he "Number" series, there are two numbers missing; 4 and 8. I want, in the first place, to find the lowest of the two, i e number 4 in this example.

I have tried to make a query like $SQL = "SELECT * FROM table_numbers WHERE ..." but I don't know how to continue. Please, Gurus of SQL, help me out. If it isn't to much to ask, please comment or explain briefly on the query you suggest. Thanks.
0
Comment
Question by:lericson
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 35152955
Does it always start at 1? If there is no 1 is that considered a missing number?
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35152959
This is an interesting question.  Here is an extended discussion along with several proposed solutions, none of which are perfect:

http://www.xaprb.com/blog/2005/12/06/find-missing-numbers-in-a-sequence-with-sql/

Do not feel bad.  Even the SQL gurus are stumped by this one.
0
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 35152982
Therefore I'm trying to define this a little better. For example If what we are really doing is looking for the lowest missing integer starting at 1, that's much easier than looking for a gap in a generic sequence. By adding a rowcount column or a not in (create integer list here) it can be solved much simpler.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 40

Expert Comment

by:Sharath
ID: 35152984
try this/
SELECT MIN(rownum) min_Missing_Number 
  FROM (  SELECT id, 
                 Number, 
                 Name, 
                 @rownum := @rownum + 1 AS rownum 
            FROM table_number, 
                 (SELECT @rownum := 0) r 
        ORDER BY Number) t1 
 WHERE Number <> rownum;

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35152985
Here are a couple of people who think they found good solutions to this query:

http://bytes.com/topic/sql-server/answers/511668-query-find-missing-number

select (a.col1 + 1)
from tab1 a
where not exists
(select 1
from tab1 b
where b.col1 = (a.col1 + 1))
and a.col1 not in
(select max(c.col1)
from tab1 c)
order by 1

Open in new window

0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 35152991
Tested on your sample.
select * from table_number;
+------+--------+----------+
| id   | Number | Name     |
+------+--------+----------+
|   25 |      1 | James    |
|   57 |      3 | Doris    |
|   78 |      9 | Dave     |
|   89 |      7 | Chuck    |
|  126 |      2 | Bertie   |
|  153 |     10 | Marion   |
|  198 |      5 | Veronica |
|  234 |      6 | Allen    |
+------+--------+----------+
8 rows in set (0.00 sec)

SELECT MIN(rownum) min_Missing_Number 
  FROM (  SELECT id, 
                 Number, 
                 Name, 
                 @rownum := @rownum + 1 AS rownum 
            FROM table_number, 
                 (SELECT @rownum := 0) r 
        ORDER BY Number) t1 
 WHERE Number <> rownum;
+--------------------+
| min_Missing_Number |
+--------------------+
|                  4 |
+--------------------+
1 row in set (0.00 sec)

Open in new window

0
 

Author Closing Comment

by:lericson
ID: 35153110
Superb. Thanks.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Presenting past 6 months bug 7 30
Fulfillment API php code sample 1 41
PHP Parse error: syntax error, unexpected '[' 1 25
error log using ftp 7 41
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 …
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

832 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