2017-05-22 33 views
1

在SQL表中,我有许多其他两个字段:CustomerID和TransactionType。 CustomerID是每个交易都可以重复的唯一号码。每笔交易都是'S'或'P'。如何选择在SQL中同时具有属性“S”和“P”的不同客户?

enter image description here

我要选择有两个 'S' 和SQL 'P' 的不同customerIDs。所以在上面的表格中,我的预期回报值是100和102.我该怎么做?

+0

请阅读http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557和接受的答案 –

+0

Microsoft SQL Server – J4ce

回答

2

您可以使用聚集了这一点:

select customerid 
from your_table 
where transactionType in ('S', 'P') 
group by customerid 
having count(distinct transactionType) = 2; 

你可以失去的where子句如果交易类型只能是“S”或“P”(甚至不为空)。

+0

谢谢!这很有道理! – J4ce

相关问题