根据oracle中的条件只排除1行

时间:2018-02-24 04:52:50

标签: oracle-sqldeveloper

一个表的值低于

|CIF |FACNO |FAC_NAME  |<br>
|1234|234   | ABCD     |<br>
|1234|235   | xyz      |<br>
|1244|345   | ABCD     |<br>
|1244|346   | ABCDE    |<br>
|1278|238   | xyz      |<br>

抱歉,无法将数据合成到表格中 如果对于相同的CIF,如果FAC_NAME = ABCD且fac_name = XYz,则仅为该特定CIF排除“xyz”行并显示其余

1 个答案:

答案 0 :(得分:0)

这个会返回所需的结果(至少,我认为是这样),但如此笨拙,我非常确定其他人会发布一个更好的选择。无论如何,你走了。

INTER用于区分'abcd'和'xyz'与其他FAC_NAME值(HM代表的值)。然后用它来检查某些CIF是否包含它们(即HM的总和= 1 - 1 = 0)和 - 如果是这种情况 - 它不会返回包含FAC_NAME ='xyz'的行。

SQL> with test (cif, facno, fac_name) as
  2    (select 1234, 234, 'abcd'  from dual union
  3     select 1234, 235, 'xyz'   from dual union
  4     select 1234, 236, 'ooo'   from dual union
  5     select 1244, 345, 'abcd'  from dual union
  6     select 1244, 346, 'abcde' from dual union
  7     select 1278, 238, 'xyz'   from dual
  8    ),
  9  inter as
 10    (select cif, facno, fac_name,
 11       case when fac_name = 'abcd' then 1
 12            when fac_name = 'xyz'  then -1
 13            else 100
 14       end hm
 15     from test
 16    )
 17  select cif, facno, fac_name
 18  from inter
 19  where facno not in (select facno from inter
 20                      where fac_name = 'xyz'
 21                        and cif in (select cif
 22                                    from inter
 23                                    where hm in (-1, 1)
 24                                    group by cif
 25                                    having sum(hm) = 0
 26                                   )
 27                     )
 28  order by cif, facno;

       CIF      FACNO FAC_N
---------- ---------- -----
      1234        234 abcd
      1234        236 ooo
      1244        345 abcd
      1244        346 abcde
      1278        238 xyz

SQL>