描述:
Customers
表:+---------------------+---------+ | Column Name | Type | +---------------------+---------+ | customer_id | int | | customer_name | varchar | +---------------------+---------+ customer_id 是这张表中具有唯一值的列。 customer_name 是顾客的名称。
Orders
表:+---------------+---------+ | Column Name | Type | +---------------+---------+ | order_id | int | | customer_id | int | | product_name | varchar | +---------------+---------+ order_id 是这张表中具有唯一值的列。 customer_id 是购买了名为 "product_name" 产品顾客的id。请你编写解决方案,报告购买了产品 "A","B" 但没有购买产品 "C" 的客户的 customer_id 和 customer_name,因为我们想推荐他们购买这样的产品。
返回按
customer_id
排序 的结果表。返回结果格式如下所示。
示例 1:
输入: Customers table: +-------------+---------------+ | customer_id | customer_name | +-------------+---------------+ | 1 | Daniel | | 2 | Diana | | 3 | Elizabeth | | 4 | Jhon | +-------------+---------------+ Orders table: +------------+--------------+---------------+ | order_id | customer_id | product_name | +------------+--------------+---------------+ | 10 | 1 | A | | 20 | 1 | B | | 30 | 1 | D | | 40 | 1 | C | | 50 | 2 | A | | 60 | 3 | A | | 70 | 3 | B | | 80 | 3 | D | | 90 | 4 | C | +------------+--------------+---------------+ 输出: +-------------+---------------+ | customer_id | customer_name | +-------------+---------------+ | 3 | Elizabeth | +-------------+---------------+ 解释: 只有 customer_id 为 3 的顾客购买了产品 A 和产品 B ,却没有购买产品 C 。
数据准备:
Create table If Not Exists Customers (customer_id int, customer_name varchar(30))
Create table If Not Exists Orders (order_id int, customer_id int, product_name varchar(30))
Truncate table Customers
insert into Customers (customer_id, customer_name) values ('1', 'Daniel')
insert into Customers (customer_id, customer_name) values ('2', 'Diana')
insert into Customers (customer_id, customer_name) values ('3', 'Elizabeth')
insert into Customers (customer_id, customer_name) values ('4', 'Jhon')
Truncate table Orders
insert into Orders (order_id, customer_id, product_name) values ('10', '1', 'A')
insert into Orders (order_id, customer_id, product_name) values ('20', '1', 'B')
insert into Orders (order_id, customer_id, product_name) values ('30', '1', 'D')
insert into Orders (order_id, customer_id, product_name) values ('40', '1', 'C')
insert into Orders (order_id, customer_id, product_name) values ('50', '2', 'A')
insert into Orders (order_id, customer_id, product_name) values ('60', '3', 'A')
insert into Orders (order_id, customer_id, product_name) values ('70', '3', 'B')
insert into Orders (order_id, customer_id, product_name) values ('80', '3', 'D')
insert into Orders (order_id, customer_id, product_name) values ('90', '4', 'C')
分析:
①先找出product_name='A'、product_name='B'、product_name='C'的数据
select * from Orders where product_name = 'A'select * from Orders where product_name = 'B'select * from Orders where product_name = 'C'②将product_name为A和B的两个表合并 从合并的表数据中找出不在product_name为C的表的customer_id
select t1.customer_id from (select * from Orders where product_name = 'A') t1, (select * from Orders where product_name = 'B') t2 where t1.customer_id = t2.customer_id and t1.customer_id not in (select customer_id from Orders where product_name = 'C')③最后连接customers表,注意distinct 因为一个顾客可能买多个产品 并根据题目要求排序
select distinct t1.customer_id,customer_name from t1,Customers where t1.customer_id = Customers.customer_id order by customer_id法二:
①先将两张表合并
select * from orders o left join customers c on c.customer_id = o.customer_id②再了解一个函数sum(product_name='A')什么意思?
解答:
因为sum为聚合函数 所以要看这个分组
本题是根据customer_id,customer_name分组
那么sum(product_name='A')表示在本组中如果存在product_name='A',那么就赋值1 没有为0
select *, sum(product_name = 'A')over(partition by c.customer_id)r1, sum(product_name = 'C') over(partition by c.customer_id)t2 from orders o left join customers c on c.customer_id = o.customer_id③根据此特点进行筛选找出sum(product_name = 'A')>0的、sum(product_name = 'B')>0的和sum(product_name = 'C')=0
select *, sum(product_name = 'A')over(partition by c.customer_id)r1, sum(product_name = 'C') over(partition by c.customer_id)t2 from orders o left join customers c on c.customer_id = o.customer_id group by c.customer_id, c.customer_name having sum(product_name = 'A') > 0 and sum(product_name = 'B') > 0 and sum(product_name = 'C') = 0④最后根据题目要求排序
代码:
法一:
with t1 as (select t1.customer_id
from (select * from Orders where product_name = 'A') t1,
(select * from Orders where product_name = 'B') t2
where t1.customer_id = t2.customer_id
and t1.customer_id not in
(select customer_id from Orders where product_name = 'C'))
select distinct t1.customer_id,customer_name from t1,Customers
where t1.customer_id = Customers.customer_id
order by customer_id;
法二:
select c.customer_id, c.customer_name
from orders o
left join customers c on c.customer_id = o.customer_id
group by c.customer_id, c.customer_name
having sum(product_name = 'A') > 0
and sum(product_name = 'B') > 0
and sum(product_name = 'C') = 0
order by c.customer_id;
总结:
理解sum(product_name='A')是难点 表示在同组中满足该条件就为1 不满足则为0