Re: Find Missing Numbers in a Sequence

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



To list the start of the gap you can use

SELECT A.ID+1 as GapStart
FROM YourTable as A LEFT JOIN YourTable As B
ON A.ID + 1= B.ID
WHERE B.ID is Null
AND A.ID Between 25 and 95

This will list the start of the gap.
For instance if you were checking 25 to 30 and the records were 25,28,30,31
this should return 26 and 29. Note that it doesn't detect that 27 is also
missing. The simplest solution to catch every item that is missing would be
to use an auxiliary table that contains nothing but a sequence of numbers
from 1 to the highest number you expect to see. With that the query becomes

SELECT S.TheNumber
FROM SequenceTable as S LEFT JOIN YourTable
ON S.TheNumber = YourTable.ID
WHERE S.TheNumber Between 25 and 95


--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..

"KARL DEWEY" <KARLDEWEY@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:555106A4-74CD-4000-A4FF-AF0572E07B6E@xxxxxxxxxxxxxxxx
This finds the first missing but sequence must start at 1 --
SELECT TOP 1 [ID]-1 AS Missing
FROM Your_Table AS Q
WHERE ((((SELECT COUNT(*) FROM Your_Table Q1
WHERE Q1.ID < Q.ID)+1)<>[ID]))
ORDER BY [ID]-1;

--
KARL DEWEY
Build a little - Test a little


"Gary Dolliver" wrote:

Hi everyone,
I have a table that is imported to daily, and sometimes orders are not
correctly migrated into the system. As all numbers that come in are in
numerical order, is there a way to create a query that will return a
result
set of numbers from the sequence that are missing, based on the highest
and
lowest value of the number sequence?
Help is always appreciated, thanks!
-gary


.



Relevant Pages

  • Re: How can I get a non consecutive numbers in collum.
    ... If you need the entire list of missing numbers, a sequence table is a ... there are never as many consecutive missing numbers as the size of the ... from Foobar, N ... when 4 then 'Gap starting at' ...
    (microsoft.public.sqlserver.programming)
  • Re: Find Missing Numbers in a Sequence
    ... The missing numbers have to come from somewhere, to create a table that has the entire sequence of numbers you could need, from lowest to highest. ... Now use the Unmatched Query Wizard to get all the numbers in this counter table that are not in your actual table. ... numerical order, is there a way to create a query that will return a result ...
    (microsoft.public.access.queries)
  • Re: Find missing numbers
    ... Is it possible to find missing numbers from a sequence? ... One way is to use a Self Join query to find the first value in each gap: ... A more complex query can find the entire gap, but it's late and I'm sleepy... ...
    (microsoft.public.access.formscoding)
  • Re: Most valuable poster
    ... nylonase or lactase evolution examples. ... residues), you have the ability, so you say, to tell us *exactly* what ... the average gap size is based on the size of the end product. ... recognizable sequence homologs or recognizable intermediate functions. ...
    (talk.origins)
  • Re: The last ancestor of all life
    ... binds, in an beneficial way, to a particular protein antigen sequence. ... antibody binding to the antigen will be rewarded with improved ... differences that produce the gap problem for evolution. ...
    (talk.origins)