Unix Technical Forum

Query to find a missing number

This is a discussion on Query to find a missing number within the MySQL forums, part of the Database Server Software category; --> Hello, I need to write a query to find out a set of missing number in a given sequence. ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:56 AM
Mahesh BS
 
Posts: n/a
Default Query to find a missing number



Hello,



I need to write a query to find out a set of missing number in a given
sequence.



Eg : a Column in some table has the following data



Col1

1

2

3

4

5

6

8

9

10



Here I need to write a query to find out that number 7 is missing in the
given sequence.

One possible solution is by using any loop. But I am looking out if the same
can be achieved using any query.



Thanks in advance.



Regards,

Mahesh











Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:56 AM
Giuseppe Maxia
 
Posts: n/a
Default Re: Query to find a missing number

Mahesh BS wrote:
> Hello,
>
>
>
> I need to write a query to find out a set of missing number in a given
> sequence.
>
>
>
> Eg : a Column in some table has the following data
>
>
>
> Col1
> 1
> 2
> 3
> 4
> 5
> 6
> 8
> 9
> 10
>
>
>
> Here I need to write a query to find out that number 7 is missing in the
> given sequence.
>
> One possible solution is by using any loop. But I am looking out if the same
> can be achieved using any query.
>


If you need to do this more than once, or if you have several missing numbers to find
in a large dataset, here's the method:
Create a table that contains a sequence, and use a LEFT JOIN to find the missing one.

To create a table and fill it with numbers quickly, use the method described in this article:
http://datacharmer.blogspot.com/2006...s-quickly.html

Then, you can issue a query like this:

SELECT
some_column
FROM
some_table
LEFT JOIN sequence_table ON some_table.col_id = sequence_table.col_id
WHERE
sequence_table.col_id IS NULL;

ciao
gmax

--
_ _ _ _
(_|| | |(_|>< The Data Charmer
_|
http://datacharmer.org/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:56 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com