我有这些列的CallHistory表:
CustomerId, TimeOfCall, CallResult
在CallResult中我可能有以下值: '已下订单','要求退货','技术问题','询问定价'等。
我想SELECT CustomerId FROM CallHistory WHERE
客户至少有一次'询问定价',但从未'放置订单'。
谢谢!
答案 0 :(得分:6)
SELECT DISTINCT c.CustomerId
FROM CallHistory c
WHERE c.CallResult = 'inquired about Pricing'
AND NOT EXISTS(SELECT 1
FROM CallHistory c2
WHERE c2.CustomerId = c.CustomerId
AND c2.CallResult = 'Placed Order')
答案 1 :(得分:2)
我认为你应该可以使用没有连接或子查询的单个选择来实现它
试试这个:
SELECT CustomerId
FROM CallHistory
GROUP BY CustomerId
HAVING COUNT(CASE WHEN CallResult='Inquired about Pricing' THEN 1 ELSE NULL END) > 0
AND COUNT(CASE WHEN CallResult='Placed Order' THEN 1 ELSE NULL END) = 0
答案 2 :(得分:1)
SELECT CustomerId
FROM CallHistory
WHERE CallResult = 'Inquired about Pricing'
EXCEPT
SELECT CustomerId
FROM CallHistory
WHERE CallResult = 'Placed Order';