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

news2024/9/20 0:27:20

描述:

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2095101.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

李沐讲座:大语言模型的实践经验和未来预测 | 上海交大

本文简介 本博客记录了李沐关于语言模型与职业生涯分享的精彩讲座,涵盖了大语言模型的核心要素、工程实践中的挑战,以及演讲者个人职业生涯中的心得体会。 李沐简介 李沐(Mu Li)是一位在人工智能与深度学习领域具有广泛影响力的…

电感的分类

电感作为电子电路中的重要元件,具有多种分类方式,每种类型的电感都有其独特的优缺点。以下是对电感分类及其优缺点的详细分析: 一、按工作频率分类 高频电感:适用于高频电路,具有较高的自谐振频率和较低的损耗。 优点…

【学习笔记】 陈强-机器学习-Python-Ch13 提升法

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-…

【python计算机视觉编程——图像聚类】

python计算机视觉编程——图像聚类 6.图像聚类6.1 K-means聚类6.1.2 图像聚类6.1.3 在主成分上可视化图像6.1.4 像素聚类 6.2 层次聚类6.3 谱聚类 6.图像聚类 6.1 K-means聚类 from scipy.cluster.vq import * import numpy as np from pylab import * matplotlib.rcParams[f…

第二篇——勾股定理:为什么在西方教毕达哥拉斯定理?

目录 一、背景介绍二、思路&方案三、过程1.思维导图2.文章中经典的句子理解3.学习之后对于投资市场的理解4.通过这篇文章结合我知道的东西我能想到什么? 四、总结五、升华 一、背景介绍 数学大厦的建立以及与自然科学的逻辑关系,以及他们的边界在这…

sql-libs第三关详细解答

首先看看and 12会不会正常显示 结果正常显示,说明存在引号闭合 加了一个引号,发现报错信息中还存在括号,说明sql语句中有括号,那我们还要闭合括号 现在就好了,and 11正常,and 12不正常,那就开始…

常用高性能架构模式

《从0开始学架构》里讲述了常用的高性能架构模式,这里面很多大家可能也都用过了,我应该也写过相关的技术文章。正好按照书里的思路重新梳理一次。 一、读写分离 读写分离的基本原理是将数据库读写操作分散到不同的节点上 感想: 读写分离应…

Go入门:gin框架极速搭建图书管理系统

Go入门:gin框架极速搭建图书管理系统 前言 本项目适合 Golang 初学者,通过简单的项目实践来加深对 Golang 的基本语法和 Web 开发的理解。 项目源码请私信,欢迎前往博主博客torna.top免费查看。 项目结构 D:. ├─ go.mod ├─ go.sum │ ├─ cmd │ └─ main │ …

Jenkins配置使用LDAP的用户和密码登录

# 检查配置文件是否正确 [rootlocalhost schema]# slaptest -u 62c6aafe ldif_read_file: checksum error on "/etc/openldap/slapd.d/cnconfig/olcDatabase{1}monitor.ldif" 62c6aafe ldif_read_file: checksum error on "/etc/openldap/slapd.d/cnconfig/olcD…

Java注解和JDK新特性

1. 注解 1.1. 认识注解 Annotation:JDK1.5新提供的技术 编译检查:比如SuppressWarnings, Deprecated和Override都具有编译检查的作用替代配置文件:使用反射来读取注解的信息 注解就是代码里的特殊标记,用于替代配置文件&#…

四大集合之Set

一、Set基础知识 1. Set集合 1.1 HashSet Set集合区别于其他三大集合的重要特性就是元素具有唯一性,南友们记不住这个特性的话,有个易记的方法。Set集合为什么要叫Set呢?因为Set集合的命名取自于我们小学数学里的集合论(Set Th…

SPI(硬件协议)

1 SPI硬件外设协议 2 SPI框图 3 硬件SPI数据收发流程 1 发送数据,同时接收数据,相互配合,可以实现数据流不间断 2 全双工SPI,发送和接收数据寄存器分开,可以同时进行 4 spi传输框图 1 速度快 2 速度慢,容…

软考中项拿证利器:系统集成项目管理工程师(第3版)一站通关

指尖疯编著的《系统集成项目管理工程师(适用第3版大纲)一站通关》目前现货已经上线各大电商平台,您可以在任一电商搜索《系统集成项目管理工程师(适用第3版大纲)一站通关》即刻找到。 出版中项一站通关完全是机缘巧合&…

Nginx: 性能优化之提升CPU效率以及TCP的三次握手和四次挥手

提升利用CPU的效率 1 )CPU的调度机制 现在来看下 linux中 CPU的一个调度机制 假设现在系统上有只有一颗CPU,而linux系统是一个多任务的一个操作系统 它允许我们各个不同的用户允许在同一个操作系统上执行很多个进程 单核CPU肯定不可能同时去执行这样一…

5.图论.题目2

5.图论.题目2 题目8.字符串接龙9.有向图的完全可达性10.岛屿的周长11.寻找存在的路径12.冗余连接113.冗余连接214.寻宝 题目 8.字符串接龙 题目链接 本题的直观思路如下图所示;但该题有两个问题:1.图中的线是如何连接起来的 2.如何确定起点到终点的最…

《JavaEE进阶》----4.<SpringMVC①简介、基本操作(各种postman请求)>

本篇博客讲解 MVC思想、及Spring MVC(是对MVC思想的一种实现)。 Spring MVC的基本操作、学习了六个注解 RestController注解 RequestMappering注解 RequestParam注解 RequestBody注解 PathVariable注解 RequestPart注解 MVC View(视图) 指在应⽤程序中…

数据同步的艺术:探索PostgreSQL和Redis的一致性策略

作者:后端小肥肠 🍇 我写过的文章中的相关代码放到了gitee,地址:xfc-fdw-cloud: 公共解决方案 🍊 有疑问可私信或评论区联系我。 🥑 创作不易未经允许严禁转载。 1. 前言 在当今高度数字化的世界中,应用程…

ACL学习笔记

1.ACL快速配置 需求:拒绝PC 1访问PC 3 (1)配置PC PC 1: PC 2: PC 3: (2)配置R1的接口IP信息 sys sysname R1 undo info-center enable interface GigabitEthernet0/0/0 ip address 192.168.1.1 255.255.255.0 qui…

超声波智能水表多少钱一个?

超声波智能水表的价格因品牌、功能、规格等因素而异,就拿深圳合众致达科技有限公司智能水电表厂家的超声波智能水表DN15口径产品举例,价格为399元起。具体价格需根据实际需求来确定。 一、影响价格的主要因素 -技术含量:具备远程数据传输、…

DSOJ-id12

1.保留几位小数 #include <iostream>#include <iomanip> //必须包含这个头文件using namespace std;void main( ){ double a 3.141596;cout<<fixed<<setprecision(3)<<a<<endl; //输出小数点后3位 2. 使用了未初始化的局部变量 Point* …