1
在SQL表中,我有许多其他两个字段:CustomerID和TransactionType。 CustomerID是每个交易都可以重复的唯一号码。每笔交易都是'S'或'P'。如何选择在SQL中同时具有属性“S”和“P”的不同客户?
我要选择有两个 'S' 和SQL 'P' 的不同customerIDs。所以在上面的表格中,我的预期回报值是100和102.我该怎么做?
在SQL表中,我有许多其他两个字段:CustomerID和TransactionType。 CustomerID是每个交易都可以重复的唯一号码。每笔交易都是'S'或'P'。如何选择在SQL中同时具有属性“S”和“P”的不同客户?
我要选择有两个 'S' 和SQL 'P' 的不同customerIDs。所以在上面的表格中,我的预期回报值是100和102.我该怎么做?
您可以使用聚集了这一点:
select customerid
from your_table
where transactionType in ('S', 'P')
group by customerid
having count(distinct transactionType) = 2;
你可以失去的where子句如果交易类型只能是“S”或“P”(甚至不为空)。
谢谢!这很有道理! – J4ce
请阅读http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557和接受的答案 –
Microsoft SQL Server – J4ce