【发布时间】:2021-08-18 13:09:16
【问题描述】:
select customer_id, currency, price
from sales_table
customer_id currency price
1001 CHF 50
1001 EUR 35
1002 EUR 40
1003 AUD 25
我的表中有数十万笔交易。 如何执行仅返回以两种不同货币付款的客户的选择子句? 在这种情况下,select 语句应该返回 customer_id 1001。
【问题讨论】:
-
考虑使用having子句,但不知道如何从那里移开
-
GROUP BY, HAVING count(distinct currency) > 1
-
在这种情况下,为什么要计算 distinct 而不是 count(currency)?
-
你需要 DISTINCT 因为否则如果你只是用欧元支付 2 次,也会显示出来
-
您可以将行 (1003, AUD, 10) 添加到您的样本数据中,以改进它,并确保更好的质量答案。
标签: sql postgresql