Reputation: 1938
I have a table orders that keeps all order from all our stores. I wrote a query to check the sequence orders for each store. It looks like that.
select WebStoreID, min(webordernumber), max(webordernumber), count(webordernumber)
from orders
where ordertype = 'WEB'
group by WebStoreID
I can check it all orders are present with this query. web ordernumber is number from 1...n.
How can I write query to find missing orders without joining to temporary/different table?
Upvotes: 2
Views: 16894
Reputation: 21
If you have the rank() function but not the lag() function (in other words, SQL Server), you can use this (suggested by http://www.sqlmonster.com/Uwe/Forum.aspx/sql-server-programming/10594/Return-gaps-in-a-sequence):
create table test_gaps_in_sequence (x int)
insert into test_gaps_in_sequence values ( 1 )
insert into test_gaps_in_sequence values ( 2 )
insert into test_gaps_in_sequence values ( 4 )
insert into test_gaps_in_sequence values ( 5 )
insert into test_gaps_in_sequence values ( 8 )
insert into test_gaps_in_sequence values ( 9 )
insert into test_gaps_in_sequence values ( 12)
insert into test_gaps_in_sequence values ( 13)
insert into test_gaps_in_sequence values ( 14)
insert into test_gaps_in_sequence values ( 29)
...
select lower_bound
, upper_bound
from (select upper_bound
, rank () over (order by upper_bound) - 1 as upper_rank
from (SELECT x+n as upper_bound
from test_gaps_in_sequence
, (SELECT 0 n
UNION
SELECT -1
) T
GROUP BY x+n
HAVING MAX(n) = -1
) upper_1
) upper_2
, (select lower_bound
, rank () over (order by lower_bound) as lower_rank
from (SELECT x+n as lower_bound
from test_gaps_in_sequence
, (SELECT 0 n
UNION
SELECT 1
) T
GROUP BY x+n
HAVING MIN(n) = 1
) lower_1
) lower_2
where upper_2.upper_rank = lower_2.lower_rank
order by lower_bound
... or, to include the "outer limits":
select lower_bound
, upper_bound
from (select upper_bound
, rank () over (order by upper_bound) - 1 as upper_rank
from (SELECT x+n as upper_bound
from test_gaps_in_sequence
, (SELECT 0 n
UNION
SELECT -1
) T
GROUP BY x+n
HAVING MAX(n) = -1
) upper_1
) upper_2
full join (select lower_bound
, rank () over (order by lower_bound) as lower_rank
from (SELECT x+n as lower_bound
from test_gaps_in_sequence
, (SELECT 0 n
UNION
SELECT 1
) T
GROUP BY x+n
HAVING MIN(n) = 1
) lower_1
) lower_2
on upper_2.upper_rank = lower_2.lower_rank
order by coalesce (lower_bound, upper_bound)
Upvotes: 2
Reputation: 132570
If your database supports analytic functions then you could use a query something like:
select prev+1, curr-1 from
( select webordernumber curr,
coalesce (lag(webordernumber) over (order by webordernumber), 0) prev
from orders
)
where prev != curr-1;
The output will show the gaps e.g.
prev+1 curr-1
------ ------
3 7
would mean that numbers 3 to 7 inclusive are missing.
Upvotes: 1
Reputation: 238076
You could join the table on itself to detect rows which have no previous row:
select cur.*
from orders cur
left join orders prev
on cur.webordernumber = prev.webordernumber + 1
and cur.webstoreid = prev.webstoreid
where cur.webordernumber <> 1
and prev.webordernumer is null
This would detect gaps in the 1...n sequence, but it would not detect duplicates.
Upvotes: 6
Reputation: 881595
I would make an auxiliary table of "all integers from 1 to n" (see http://www.sql-server-helper.com/functions/integer-table.aspx for some ways to make it with a SQL Server function, but since it's something you will need over and over I'd make it into a real table anyway, and with any SQL engine it's easy to make that, just once) then use a nested query, SELECT value FROM integers WHERE value NOT IN (SELECT webordernumber FROM orders)
&c. Also see http://www.sqlmag.com/Article/ArticleID/99797/sql_server_99797.html for a problem similar to yours, "detecting gaps in a sequence of numbers".
Upvotes: 4