如何返回没有结果?

时间:2018-02-09 11:42:37

标签: sql oracle

我有以下数据集

1   CCID        CEID    COM_ID  OSN_zasl_5_special
2   195540560   3863357 28810   INFO_support
3   197562575   3863357 8307    LIST_zav_splatky
4   197307065   3863357 29760   OSN_dat_prvej_spl
5   197307066   3863357 8508    CS_sprava_kod 6
6   198284541   3863357 8508    CS_sprava_kod 6
7   198297147   3863357 29030   CS_sprava_kod 54
8   199288740   3863357 2121    PA02

这就是我需要的,如果" ceid"有id 8307(com_id)的评论没有返回结果。换句话说,我有这个查询

select table.ceid from table
where table.com_id != 8307

但是此查询将返回此ceid,因为存在多个具有相同ceid且不包含此注释ID的条目。

我试图在com_id上使用ceid使用stragg进行分组,然后stragg(com_id)不喜欢' com_id'但这似乎没有做到这一点。有吗

2 个答案:

答案 0 :(得分:1)

一种方法使用not exists

select t.*
from t
where not exists (select 1 from t t2 where t2.ceid = t.ceid and t2.com_id = 8307);

有时,使用窗口函数执行此操作会很有用:

select t.*
from (select t.*,
             sum(case when com_id = 8307 then 1 else 0 end) over (partition by ceid) as cnt_8307
      from t
     ) t
where cnt_8307 = 0;

答案 1 :(得分:0)

考虑一下:

select table.ceid from table
minus
select table.ceid from table
where table.com_id = 8307