我如何使用SQL找到系列中缺少的元素?

Bha*_*ana 0 sql oracle10g

我有一个bill列名为bill_id的表.bill_id值范围是1到40.我有像行

bill_id
-----------
1  
3  
6  
8  
2  
21  
34  
35  
26  
40
Run Code Online (Sandbox Code Playgroud)

如何找到缺失的元素(4,5,7,9,10等)?

Rob*_*ijk 8

SQL> create table bill (bill_id)
  2  as
  3  select 1 from dual union all
  4  select 3 from dual union all
  5  select 6 from dual union all
  6  select 8 from dual union all
  7  select 2 from dual union all
  8  select 21 from dual union all
  9  select 34 from dual union all
 10  select 35 from dual union all
 11  select 26 from dual union all
 12  select 40 from dual
 13  /

Table created.

SQL> with all_possible_bill_ids as
  2  ( select level bill_id
  3      from dual
  4   connect by level <= 40
  5  )
  6  select bill_id
  7    from all_possible_bill_ids
  8   minus
  9  select bill_id
 10    from bill
 11  /

   BILL_ID
----------
         4
         5
         7
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        22
        23
        24
        25
        27
        28
        29
        30
        31
        32
        33
        36
        37
        38
        39

30 rows selected.
Run Code Online (Sandbox Code Playgroud)

问候,
Rob.