查询以从表中选择具有特定列值的记录

时间:2019-12-30 12:54:13

标签: sql oracle

enter image description here [详细说明]我有一张钞票表,因此我不想显示有Doctyp ='BIL'的记录

BILNO        DOCTYP

1812B00001      BIL 
1812B00001      RCR 
ADVN            CN 
ADVN            DA 
ADVN            RCD 
ADVN            RCR 
ADVN            TF 
AL1707B00006    BIL 
AL1707B00006    RCR

我希望输出类似

BILNO   DOCTYP

ADVN    CN 
ADVN    DA 
ADVN    RCD 
ADVN    RCR 
ADVN    TF

1 个答案:

答案 0 :(得分:2)

NOT IN是一种选择:

SQL> with test (bilno, doctyp) as
  2    (select 182, 'bil' from dual union all
  3     select 182, 'xxy' from dual union all
  4     select 111, 'abc' from dual union all
  5     select 111, 'zdv' from dual union all
  6     select 223, 'bil' from dual union all
  7     select 555, 'xzy' from dual
  8    )
  9  select *
 10  from test t
 11  where bilno not in (select bilno
 12                      from test
 13                      where doctyp = 'bil');

     BILNO DOC
---------- ---
       111 zdv
       111 abc
       555 xzy

另一个是NOT EXISTS

SQL> with test (bilno, doctyp) as
  2    (select 182, 'bil' from dual union all
  3     select 182, 'xxy' from dual union all
  4     select 111, 'abc' from dual union all
  5     select 111, 'zdv' from dual union all
  6     select 223, 'bil' from dual union all
  7     select 555, 'xzy' from dual
  8    )
  9  select *
 10  from test t
 11  where not exists (select null
 12                    from test t1
 13                    where t1.bilno = t.bilno
 14                      and t1.doctyp = 'bil'
 15                   );

     BILNO DOC
---------- ---
       111 zdv
       111 abc
       555 xzy