SQL:报告仅存在两个可能值之一的实例

时间:2015-09-23 19:12:59

标签: sql postgresql

我一直试图解决这个问题...... 我有两个数据集。 security_account.name和spot.format。 我需要找出哪些帐户只包含价值为' SD'。

的广告

此查询:

select distinct
sa.name as "Sender Account"
from
security_account sa,
spot s
where
sa.id = s.provider_account_id
and s.format = 'SD';

如果我说的话,给我一个有SD点的每个帐户的列表!=' HD',我得到相同的结果。大多数帐户都有HD和SD,我只想在现货表中看到仅包含SD的帐户。

有什么想法吗?

谢谢你,马特。

2 个答案:

答案 0 :(得分:0)

使用except

select sa.name as "Sender Account"
from security_account sa
join spot s on sa.id = s.provider_account_id
where s.format = 'SD'
except
select sa.name as "Sender Account"
from security_account sa
join spot s on sa.id = s.provider_account_id
where s.format <> 'SD'

答案 1 :(得分:0)

一种方法是使用existsnot exists

select sa.name as "Sender Account"
from security_account sa
where exists (select *
               from spot s
              where s.provider_account_id = sa.id
                and s.format = 'SD')
  and not exists (select *
                   from spot s
                  where s.provider_account_id = sa.id
                    and s.format = 'HD')