建设银行在上海的招聘网站,北京建站程序,建设网站的基本技术,成都个人做网站大家好#xff0c;我是空空star#xff0c;本篇带大家了解一道简单的力扣sql练习题。 文章目录前言一、题目#xff1a;1084. 销售分析III二、解题1.正确示范①提交SQL运行结果2.正确示范②提交SQL运行结果3.正确示范③提交SQL运行结果4.正确示范④提交SQL运行结果5.其他总结… 大家好我是空空star本篇带大家了解一道简单的力扣sql练习题。 文章目录前言一、题目1084. 销售分析III二、解题1.正确示范①提交SQL运行结果2.正确示范②提交SQL运行结果3.正确示范③提交SQL运行结果4.正确示范④提交SQL运行结果5.其他总结前言 一、题目1084. 销售分析III
Table: Product
-----------------------
| Column Name | Type |
-----------------------
| product_id | int |
| product_name | varchar |
| unit_price | int |
-----------------------
Product_id是该表的主键。
该表的每一行显示每个产品的名称和价格。
Table: Sales
----------------------
| Column Name | Type |
----------------------
| seller_id | int |
| product_id | int |
| buyer_id | int |
| sale_date | date |
| quantity | int |
| price | int |
------ ---------------
这个表没有主键它可以有重复的行。
product_id 是 Product 表的外键。
该表的每一行包含关于一个销售的一些信息。
编写一个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 |
--------------------------------------------------------------
输出
---------------------------
| product_id | product_name |
---------------------------
| 1 | S8 |
---------------------------
解释: id为1的产品仅在2019年春季销售。 id为2的产品在2019年春季销售但也在2019年春季之后销售。 id 3的产品在2019年春季之后销售。 我们只退回产品1因为它是2019年春季才销售的产品。
二、解题
1.正确示范①
提交SQL
select product_id,product_name from Product
where product_id in(select product_id from Saleswhere sale_date between 2019-01-01 and 2019-03-31
)
and product_id not in (select product_id from Saleswhere substr(sale_date,1,7)2019-04or substr(sale_date,1,7)2019-01
);运行结果 2.正确示范②
提交SQL
select distinct u1.product_id,u1.product_name
from Product u1
left join Sales u2
on u1.product_idu2.product_id
and u2.sale_date between 2019-01-01 and 2019-03-31
left join Sales u3
on u1.product_idu3.product_id
and (substr(u3.sale_date,1,7)2019-04
or substr(u3.sale_date,1,7)2019-01)
where u2.product_id is not null
and u3.product_id is null;运行结果 3.正确示范③
提交SQL
select u1.product_id,u1.product_name
from Product u1
left join Sales u2
on u1.product_idu2.product_id
and u2.sale_date between 2019-01-01 and 2019-03-31
left join Sales u3
on u1.product_idu3.product_id
and (substr(u3.sale_date,1,7)2019-04
or substr(u3.sale_date,1,7)2019-01)
group by u1.product_id,u1.product_name
having count(u2.product_id)0 and count(u3.product_id)0;运行结果 4.正确示范④
提交SQL
select u1.product_id,u1.product_name
from Product u1
left join Sales u2
on u1.product_idu2.product_id
group by u1.product_id,u1.product_name
having min(u2.sale_date)2019-01-01
and max(u2.sale_date)2019-03-31;运行结果 5.其他 总结 正确示范①思路 限定product_id in 2019年春季销售过 and product_id not in 2019年春季之外销售过的product_id 正确示范②思路 将 in 和 not in 转换为 left join 的形式 在最后限定u2.product_id is not null and u3.product_id is null在获取结果时加上 distinct 去重 正确示范③思路 将 in 和 not in 转换为 left join 的形式 按group by u1.product_id,u1.product_name 分组并去重再通过 having count(u2.product_id)0 and count(u3.product_id)0筛选 正确示范④思路 按group by u1.product_id,u1.product_name 分组并去重通过having min(u2.sale_date)‘2019-01-01’ and max(u2.sale_date)‘2019-03-31’ 筛选