![](/img/trans.png)
[英]Find pair of customers(CustID), who have bought same number of items.(SQL Nested Query)
[英]Customers who bought the exact 2 items
我有一个名为order_details
的表,其中包含 3 列order_id
、 customer_id
和item_name
我需要找到customer_id
那些谁买了两个客户第Kindle
和Alexa
这就是我的桌子的样子。
这些是供您参考的 DML 和 DDL 命令:
create table personal.order_details(
order_id varchar(10),
customer_id varchar(10),
item_name varchar(10))
insert into personal.order_details values
('1000','C01','Alexa'),
('1000','C01','Kindle'),
('1001','C02','Alexa'),
('1002','C03','Alexa'),
('1002','C03','Kindle')
提前致谢。
您可以使用聚合和having
子句:
select od.customer_id
from personal.order_details od
where od.item_name in ('Alexa', 'Kindle') -- rows that have either
group by od.customer_id
having count(distinct od.item_name) = 2; -- customers that have both
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.