如何查询没有组合数据的所有行

时间:2017-08-31 14:22:17

标签: oracle11g

我正在处理的数据如下所示

abc  year  flag
---  ----  ----
a    2001  Y
a    2001  N
a    2001  N
b    2002  Y
b    2002  N
c    2002  N
m    2001  Y
d    2003  N

我需要所有没有旗帜的行' Y'为了' abc'和'年'组合

结果数据应为:

abc  year  flag
---  ----  ---- 
c    2002  N
d    2003  N

1 个答案:

答案 0 :(得分:0)

这是ANTI-JOIN的一个简单应用:

with
     inputs ( abc, yr, flag ) as (
       select 'a', 2001, 'Y' from dual union all 
       select 'a', 2001, 'N' from dual union all
       select 'a', 2001, 'N' from dual union all
       select 'b', 2002, 'Y' from dual union all
       select 'b', 2002, 'N' from dual union all
       select 'c', 2002, 'N' from dual union all
       select 'm', 2001, 'Y' from dual union all
       select 'd', 2003, 'N' from dual
     )
-- End of simulated inputs (for testing purposes only, not part of the query).
-- SQL solution begins BELOW THIS LINE. Use your actual table and column names.
select abc, yr, flag
from   inputs
where  (abc, yr) not in (select abc, yr from inputs where flag = 'Y')
;

ABC    YR  FLAG
---  ----  ----
c    2002  N
d    2003  N