当前位置: 首页 > news >正文

LeetCode_sql_day20(1398.购买了产品A和产品B却没有购买产品C的顾客)

描述:

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_idfrom (select * from Orders where product_name = 'A') t1,(select * from Orders where product_name = 'B') t2where t1.customer_id = t2.customer_idand 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 oleft 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 oleft 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 oleft join customers c on c.customer_id = o.customer_id
group by c.customer_id, c.customer_name
having sum(product_name = 'A') > 0and sum(product_name = 'B') > 0and sum(product_name = 'C') = 0

④最后根据题目要求排序

代码:

法一:
with t1 as (select t1.customer_idfrom (select * from Orders where product_name = 'A') t1,(select * from Orders where product_name = 'B') t2where t1.customer_id = t2.customer_idand 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 oleft join customers c on c.customer_id = o.customer_id
group by c.customer_id, c.customer_name
having sum(product_name = 'A') > 0and sum(product_name = 'B') > 0and sum(product_name = 'C') = 0
order by c.customer_id;

总结:

理解sum(product_name='A')是难点 表示在同组中满足该条件就为1 不满足则为0


http://www.mrgr.cn/news/16971.html

相关文章:

  • 大模型辅助软件开发,助力工程师的开发之路
  • sqli-labs靶场通关攻略(51-60)
  • 深入探索C语言中的各种Sleep方法
  • 一分钟学会系列-1电烙铁(焊台)
  • 【惊天BUG】select * from 表 where value = 0;查询结果让大师兄都傻眼了
  • 大模型web服务部署—lobe-chat 部署
  • 6种方法:如何克服编程挫折,找到突破的学习方法
  • 折叠cell的学习
  • 【Python百日进阶-Web开发-Peewee】Day298 - 骇客 Hacks
  • 计算机毕业设计选题推荐-公司考勤管理系统-Java/Python项目实战
  • python 地理抠图
  • 爬虫常用模板
  • 足浴城消费系统小程序的设计
  • Python知识点:如何使用Coverage进行代码覆盖率分析
  • 中国文化艺术孙溟展浅析《绛帖》
  • 音视频开发之旅(90)-Vision Transformer论文解读与源码分析
  • 文心快码前端工程师观点分享:人机协同新模式的探索之路(一)
  • 设计模式之中介者模式
  • 常用排序算法(上)
  • 0901python打印异常信息