1083. 销售分析 II-sql

该SQL查询旨在找出只购买了S8但未购买iPhone的买家ID。通过左连接Sales和Product表,然后按buyer_id分组并计算每个产品的总数量,确保S8的数量大于0且iPhone的数量等于0。另一种方法是使用子查询来分别找出购买S8和iPhone的buyer_id,然后进行比较和排除。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

-- better one
select buyer_id   
from Sales left outer join Product on Sales.product_id = Product.product_id 
group by buyer_id having sum(product_name='S8')>0 and sum(product_name='iPhone')=0;


-- mine
select distinct buyer_id from Sales
where buyer_id in 
(select distinct buyer_id from Sales as s inner join Product as p on s.product_id = p.product_id where product_name = 'S8') 
and buyer_id not in 
(select distinct buyer_id from Sales as s inner join Product as p on s.product_id = p.product_id where product_name = 'iPhone') ;

-- better one

select buyer_id   

from Sales left outer join Product on Sa

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值