【发布时间】:2020-10-21 06:30:09
【问题描述】:
我有两张桌子:
Sales
columns: (Sales_id, Date, Customer_id, Product_id, Purchase_amount):
Product
columns: (Product_id, Product_Name, Brand_id,Brand_name)
我必须编写一个查询来查找购买了品牌“X”和“Y”(两者)以及每个品牌至少 2 件产品的客户。以下查询是否正确?有什么建议的更改吗?
SELECT S.Customer_id "Customer ID"
FROM Sales S LEFT JOIN Product P
ON S.Product_id = P.Product_id
AND P.Brand_Name IN ('X','Y')
GROUP BY S.Customer_id
HAVING COUNT(DISTINCT S.Product_id)>=2 -----at least 2 products in each brand
AND COUNT(S.Customer_id) =2 ---------------customers who bought both brands
任何帮助将不胜感激。提前致谢
【问题讨论】:
标签: sql oracle select count db2