数据

产品表:Product+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| product_id   | int     |
| product_name | varchar |
| unit_price   | int     |
+--------------+---------+
product_id 是这个表的主键.
销售表:Sales+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| seller_id   | int     |
| product_id  | int     |
| buyer_id    | int     |
| sale_date   | date    |
| quantity    | int     |
| price       | int     |
+------ ------+---------+
这个表没有主键,它可以有重复的行.
product_id 是 Product 表的外键.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sales-analysis-i
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

1082. 销售分析 I

DROP table if exists Product, Sales;
Create table If Not Exists Product (product_id int, product_name varchar(10), unit_price int);
Create table If Not Exists Sales (seller_id int, product_id int, buyer_id int, sale_date date, quantity int, price int);
Truncate table Product;
insert into Product (product_id, product_name, unit_price) values ('1', 'S8', '1000');
insert into Product (product_id, product_name, unit_price) values ('2', 'G4', '800');
insert into Product (product_id, product_name, unit_price) values ('3', 'iPhone', '1400');
Truncate table Sales;
insert into Sales (seller_id, product_id, buyer_id, sale_date, quantity, price) values ('1', '1', '1', '2019-01-21', '2', '2000');
insert into Sales (seller_id, product_id, buyer_id, sale_date, quantity, price) values ('1', '2', '2', '2019-02-17', '1', '800');
insert into Sales (seller_id, product_id, buyer_id, sale_date, quantity, price) values ('2', '2', '3', '2019-06-02', '1', '800');
insert into Sales (seller_id, product_id, buyer_id, sale_date, quantity, price) values ('3', '3', '4', '2019-05-13', '2', '2800');

题目

编写一个 SQL 查询,查询总销售额最高的销售者,如果有并列的,就都展示出来。

查询结果格式如下所示:

Product 表:
+------------+--------------+------------+
| product_id | product_name | unit_price |
+------------+--------------+------------+
| 1          | S8           | 1000       |
| 2          | G4           | 800        |
| 3          | iPhone       | 1400       |
+------------+--------------+------------+Sales 表:
+-----------+------------+----------+------------+----------+-------+
| seller_id | product_id | buyer_id | sale_date  | quantity | price |
+-----------+------------+----------+------------+----------+-------+
| 1         | 1          | 1        | 2019-01-21 | 2        | 2000  |
| 1         | 2          | 2        | 2019-02-17 | 1        | 800   |
| 2         | 2          | 3        | 2019-06-02 | 1        | 800   |
| 3         | 3          | 4        | 2019-05-13 | 2        | 2800  |
+-----------+------------+----------+------------+----------+-------+Result 表:
+-------------+
| seller_id   |
+-------------+
| 1           |
| 3           |
+-------------+
Id 为 1 和 3 的销售者,销售总金额都为最高的 2800。

解题记录

  • 通过销售id聚合得到员工销售总额
  • 然后筛选出销售总额和最高销售总额相同的员工
with t as (select seller_id, sum(price) total from Sales group by seller_id)
select seller_id
from t
where total = (select max(total) from t)

1083. 销售分析 II

题目

编写一个 SQL 查询,查询购买了 S8 手机却没有购买 iPhone 的买家。注意这里 S8 和 iPhone 是 Product 表中的产品。

查询结果格式如下图表示:

Product table:
+------------+--------------+------------+
| product_id | product_name | unit_price |
+------------+--------------+------------+
| 1          | S8           | 1000       |
| 2          | G4           | 800        |
| 3          | iPhone       | 1400       |
+------------+--------------+------------+Sales table:
+-----------+------------+----------+------------+----------+-------+
| seller_id | product_id | buyer_id | sale_date  | quantity | price |
+-----------+------------+----------+------------+----------+-------+
| 1         | 1          | 1        | 2019-01-21 | 2        | 2000  |
| 1         | 2          | 2        | 2019-02-17 | 1        | 800   |
| 2         | 1          | 3        | 2019-06-02 | 1        | 800   |
| 3         | 3          | 3        | 2019-05-13 | 2        | 2800  |
+-----------+------------+----------+------------+----------+-------+Result table:
+-------------+
| buyer_id    |
+-------------+
| 1           |
+-------------+
id 为 1 的买家购买了一部 S8,但是却没有购买 iPhone,而 id 为 3 的买家却同时购买了这 2 部手机。

解题记录

  • 优先合并表将产品名称合并,并通过买者id和产品名称去重
  • 然后通过设置分数来再求和来区分产品情况,类似2进制表示,S8:10, iphone:1
  • 10就是只有S8没有iphone,11就是有s8且有iphone,1就是只有iphone,0就是都没有
with t as (select s.buyer_id, p.product_name from Sales s inner join Product p on s.product_id = p.product_id group by s.buyer_id, p.product_name),t1 as (select buyer_id, case when product_name='S8' then 10 when product_name='iPhone' then 1 else 0 end cost from t),t2 as (select buyer_id, sum(cost) cost from t1 group by buyer_id)
select buyer_id from t2 where cost=10

1084. 销售分析III

题目

编写一个SQL查询,报告2019年春季才售出的产品。即仅在2019-01-01至2019-03-31(含)之间出售的商品。

查询结果格式如下所示:

Product table:
+------------+--------------+------------+
| product_id | product_name | unit_price |
+------------+--------------+------------+
| 1          | S8           | 1000       |
| 2          | G4           | 800        |
| 3          | iPhone       | 1400       |
+------------+--------------+------------+Sales table:
+-----------+------------+----------+------------+----------+-------+
| seller_id | product_id | buyer_id | sale_date  | quantity | price |
+-----------+------------+----------+------------+----------+-------+
| 1         | 1          | 1        | 2019-01-21 | 2        | 2000  |
| 1         | 2          | 2        | 2019-02-17 | 1        | 800   |
| 2         | 2          | 3        | 2019-06-02 | 1        | 800   |
| 3         | 3          | 4        | 2019-05-13 | 2        | 2800  |
+-----------+------------+----------+------------+----------+-------+Result table:
+-------------+--------------+
| product_id  | product_name |
+-------------+--------------+
| 1           | S8           |
+-------------+--------------+
id为1的产品仅在2019年春季销售,其他两个产品在之后销售。

解题记录

  • 通过对时间判断获取是否在产品春季销售并去重
  • 然后通过获取只在春季销售的产品,然后通过join获取产品名称
with t as (select product_id, IF((sale_date between DATE '2019-01-01' and DATE '2019-03-31'), 10, 1) inSpringfrom Salesgroup by product_id, inSpring),t2 as (select product_id, sum(inSpring) cost from t group by product_id)
select t2.product_id, p.product_name
from t2left join Product p on t2.product_id = p.product_id
where cost = 10

SQL:1082. 销售分析 1-3相关推荐

  1. LeetCode的SQL题练手(MySQL实现)

    本文系在 LeetCode 刷题所做的笔记,大部分为简单题,旨在复习熟悉SQL写法,如有不当之处欢迎通过QQ指教. Contens 175 组合两个表 官方解法 备注 181 超过经理收入的员工 我的 ...

  2. LeetCode数据库SQL题目记录(难度:简单)

    难度:简单 目录 175. 组合两个表 176. 第二高的薪水 181. 超过经理收入的员工 182. 查找重复的电子邮箱 183. 从不订购的客户 196. 删除重复的电子邮箱 197. 上升的温度 ...

  3. LeetCode刷SQL题

    https://leetcode-cn.com/problemset/database/ 题目都是leetcode 上了可以点击题目会有相应的链接 由于个人比较喜欢用开窗函数,所以都优先用了开窗 ,当 ...

  4. LeetCode MySQL解题目录

    已完成的 LeetCode MySQL 数据库题目.点击查看我的 LeetCode 算法解题目录. 已解决 123/123 - 简单 54 中等 51 困难 18 前置入门学习 MySQL 基本查询. ...

  5. MySQL 排序、分页查询、聚合查询

    文章目录 1. 排序 2. 分页查询 3. 聚合查询 3.1 分组聚合 GROUP BY 练习 LeetCode 176. 第二高的薪水 练习 LeetCode 177. 第N高的薪水 练习 Leet ...

  6. 【leetcode-sql】1082-1084、1097

    产品表:Product +--------------+---------+ | Column Name | Type | +--------------+---------+ | product_i ...

  7. LeetCode数据库题目1-123

    LeetCode数据库题目1-123 175. 组合两个表 难度简单 SQL架构 表1: Person +-------------+---------+ | 列名 | 类型 | +--------- ...

  8. LeetCode-数据库题(二) (52-125题 到1565)

    文章目录 [1075. 项目员工 I](https://leetcode-cn.com/problems/project-employees-i/) [1076. 项目员工II](https://le ...

  9. leetcode数据库题目1-123题(20-08-14)(1)

    难度简单 SQL架构 表1: Person +-------------+---------+ | 列名 | 类型 | +-------------+---------+ | PersonId | i ...

最新文章

  1. 【基础巩固篇】Java中String揭秘!
  2. RP2836 OUT0-OUT7 对应关系
  3. python程序员需要掌握哪些技术-程序员Python编程必备5大工具,你用过几个?
  4. C++ - RTTI(RunTime Type Information)运行时类型信息 详解
  5. Spring和SpringMVC的父子容器关系
  6. 四十三、在Vue使用router,路由的管理
  7. 222. 完全二叉树的节点个数 golang
  8. TortoiseSVN--Subversion客户端使用详解及问题解决
  9. 26. 平衡二叉排序树
  10. 【转】python技术博客
  11. 将Windows驱动程序从一台计算机复制到另一台计算机
  12. 针对Mrpt/build中的make时u出现的问题ccache: error: Failed to create temporary file for /home/jyy/.ccache/tmp/tm
  13. [Warning] World-writable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored.
  14. mac远程登录虚拟机
  15. 《计算机网络》虚拟局域网和高速以太网
  16. mosquitto 群晖下载_从此“不再”登陆PT站!RSS订阅+qbittorrent自动下载使用教程
  17. 对diff算法的理解
  18. 字符串—StringBuilder
  19. CAS-搭建CAS Server服务端(静态认证)
  20. Spring Data JPA OneToMany级联,多方删除修改新增详解(好文章!!申精!!)

热门文章

  1. LTspice基础教程-007.voltage电压源基本设置
  2. AI校园来了,你准备好了吗?
  3. 一所小学校组建的计算机网络系统属于( ),现代教育技术基础与应用答案与试题(最新)...
  4. 【笔记1】关于联想g470更换intel5300网卡的问题
  5. Java修炼之凡界篇 筑基期 第05卷 数组 番外1 稀疏数组
  6. ArcGIS滑坡易发性及危险性评价实验文字说明
  7. 郑州医疗卫生服务迈入大数据时代
  8. 让地球的实时美丽照片显示在你的Mac桌面上-即刻地球 mac中文版
  9. 新 Spring Cloud (一) 之 Eureka 服务注册中心
  10. SQL 2017 SQLPS执行Add-SqlAvailabilityDatabase异常