13,SQL训练之:力扣,1084,销售分析III

目录

一,原题力扣链接

二,题干

三,建表语句

四,分析

五,SQL解答

解法一,子查询

 解法二,with上下文  内连接

六,验证

七,知识点总结


一,原题力扣链接

. - 力扣(LeetCode)

二,题干

表: 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 表的外键(reference 列)。
该表的每一行包含关于一个销售的一些信息。

编写解决方案,报告 2019年春季 才售出的产品。即 仅 在 2019-01-01 (含)至 2019-03-31 (含)之间出售的商品。

以 任意顺序 返回结果表。

结果格式如下所示。

示例 1:

输入:
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 年春季之后销售。
我们只返回 id 为 1 的产品,因为它是 2019 年春季才销售的产品。

三,建表语句

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');

四,分析

第一个表的结构  产品表  有产品id  有产品名称 有产品单价
第二个表的结构  有卖家id 有产品id 有买家id  有交易时间  有交易数量 有交易价格

题目要求  2019年从春季  春季  即2019-01-01到2019-3-31 之间的商品id和名称

简单查询 拼接2个表 拿到商品id和商品名称即可  条件是春季
方案一  写死的春季天数
方案二 季度函数

坑点:
仅再春节销售的  如果不是再春季销售的要筛选出来  所以就需要对产品id分组了

以产品id分组 去最小的天数和最大的天数 出来 
最小的天数 如果再春季的第一天
最大的天数 如果再春季的最后一天  
才算 这样他的就全部都在春季销售了 这个产品

五,SQL解答

解法一,子查询

select product_id,product_name from product where product_id in (
        select s.product_id as s_id from product p,sales s
         where p.product_id=s.product_id
         group by s.product_id having min(sale_date)>='2019-1-1' and max(sale_date)<='2019-3-31'
    );

 解法二,with上下文  内连接

# 方案二
with t1 as (
    select s.product_id as s_id from product p,sales s
 where p.product_id=s.product_id
 group by s.product_id having min(sale_date)>='2019-1-1' and max(sale_date)<='2019-3-31'
)
select product_id,product_name from product,t1 where product_id=t1.s_id;

六,验证

解法二验证,内连接

解法一验证  子查询

七,知识点总结

总结:
仅仅再春节 需要对用户分组 去最小的天数和最大的天数 然后分别去和春季最后一天和第一天筛选
子查询用in 比= 要好;
子查询改写为with 连接 比较容易阅读
min函数的用法
max函数的用法
内连接的运用

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值