2012-02-15 60 views
0

我有一個包含數字的表,範圍在1-1000之間,它們是唯一的,但是我需要使用Oracle SQL從表中確定1-1000範圍內哪些數字缺失,Oracle SQL,範圍在1-1000之間

1,3,4,5,6,8,9,10

結果2,7

任何人都可以用一個腳本或URL到一個腳本幫助?

+2

11,12等等呢?他們是不是也錯過了? – 2012-02-15 16:04:19

+0

不,因爲計數還沒有那麼高,但我可以使用max(SheetNo)作爲高點。 – icecurtain 2012-02-15 16:06:53

回答

2

你可以使用:

select level from dual 
connect by level<=1000 
minus 
select mycolumn from mytable 

編輯:

要找出哪些號碼是從序列丟失,而不硬編碼1000:

select level from dual 
connect by level < (select max(mycolumn) from mytable) 
minus 
select mycolumn from mytable 
+0

+1。好主意 – 2012-02-15 16:07:48

+0

這正是我需要的感謝。 – icecurtain 2012-02-15 16:16:07

0

嘗試:

select a.check_number 
from (select level check_number from dual connect by level <= 1000) a 
where not exists 
(select null from myTable t where a.check_number = t.lookup_number) 
0
with numbers as (
    select level as i 
    from dual 
    connect by level <= (select max(some_number) from your_table) 
) 
select nr.i as missing 
from numbers nr 
    left join your_table yt on yt.some_number = nr.i 
where yt.some_number is null;