题目描述
(通过次数8,853 | 提交次数12,746,通过率69.46%)
表: Products +------------------+---------+ | Column Name | Type | +------------------+---------+ | product_id | int | | product_name | varchar | | product_category | varchar | +------------------+---------+ product_id 是该表主键。 该表包含该公司产品的数据。 表: Orders +---------------+---------+ | Column Name | Type | +---------------+---------+ | product_id | int | | order_date | date | | unit | int | +---------------+---------+ 该表无主键,可能包含重复行。 product_id 是表单 Products 的外键。 unit 是在日期 order_date 内下单产品的数目。 写一个 SQL 语句,要求获取在 2020 年 2 月份下单的数量不少于 100 的产品的名字和数目。 返回结果表单的 顺序无要求 。 查询结果的格式如下。 示例 1: 输入: Products 表: +-------------+-----------------------+------------------+ | product_id | product_name | product_category | +-------------+-----------------------+------------------+ | 1 | Leetcode Solutions | Book | | 2 | Jewels of Stringology | Book | | 3 | HP | Laptop | | 4 | Lenovo | Laptop | | 5 | Leetcode Kit | T-shirt | +-------------+-----------------------+------------------+ Orders 表: +--------------+--------------+----------+ | product_id | order_date | unit | +--------------+--------------+----------+ | 1 | 2020-02-05 | 60 | | 1 | 2020-02-10 | 70 | | 2 | 2020-01-18 | 30 | | 2 | 2020-02-11 | 80 | | 3 | 2020-02-17 | 2 | | 3 | 2020-02-24 | 3 | | 4 | 2020-03-01 | 20 | | 4 | 2020-03-04 | 30 | | 4 | 2020-03-04 | 60 | | 5 | 2020-02-25 | 50 | | 5 | 2020-02-27 | 50 | | 5 | 2020-03-01 | 50 | +--------------+--------------+----------+ 输出: +--------------------+---------+ | product_name | unit | +--------------------+---------+ | Leetcode Solutions | 130 | | Leetcode Kit | 100 | +--------------------+---------+ 解释: 2020 年 2 月份下单 product_id = 1 的产品的数目总和为 (60 + 70) = 130 。 2020 年 2 月份下单 product_id = 2 的产品的数目总和为 80 。 2020 年 2 月份下单 product_id = 3 的产品的数目总和为 (2 + 3) = 5 。 2020 年 2 月份 product_id = 4 的产品并没有下单。 2020 年 2 月份下单 product_id = 5 的产品的数目总和为 (50 + 50) = 100 。 来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/list-the-products-ordered-in-a-period
//测试数据 Create table If Not Exists Products (product_id int, product_name varchar(40), product_category varchar(40)); Create table If Not Exists Orders (product_id int, order_date date, unit int); insert into Products (product_id, product_name, product_category) values ('1', 'Leetcode Solutions', 'Book'); insert into Products (product_id, product_name, product_category) values ('2', 'Jewels of Stringology', 'Book'); insert into Products (product_id, product_name, product_category) values ('3', 'HP', 'Laptop'); insert into Products (product_id, product_name, product_category) values ('4', 'Lenovo', 'Laptop'); insert into Products (product_id, product_name, product_category) values ('5', 'Leetcode Kit', 'T-shirt'); insert into Orders (product_id, order_date, unit) values ('1', '2020-02-05', '60'); insert into Orders (product_id, order_date, unit) values ('1', '2020-02-10', '70'); insert into Orders (product_id, order_date, unit) values ('2', '2020-01-18', '30'); insert into Orders (product_id, order_date, unit) values ('2', '2020-02-11', '80'); insert into Orders (product_id, order_date, unit) values ('3', '2020-02-17', '2'); insert into Orders (product_id, order_date, unit) values ('3', '2020-02-24', '3'); insert into Orders (product_id, order_date, unit) values ('4', '2020-03-01', '20'); insert into Orders (product_id, order_date, unit) values ('4', '2020-03-04', '30'); insert into Orders (product_id, order_date, unit) values ('4', '2020-03-04', '60'); insert into Orders (product_id, order_date, unit) values ('5', '2020-02-25', '50'); insert into Orders (product_id, order_date, unit) values ('5', '2020-02-27', '50'); insert into Orders (product_id, order_date, unit) values ('5', '2020-03-01', '50');
解题思路
Products表保存了所有的产品信息。
Orders表保存了所有的订单信息。包括产品ID、下单时间、下单数量。
题目要求:查询在2020年2月份,总下单数量不少于100的产品。
那么,首先,我们需要筛选出2020年2月份的所有订单。
然后,使用产品ID进行分组汇总,计算出每个产品总的下单数量。
接着,使用HAVING子句,过滤出总下单数量不少于100的产品。
最后,使用左关联或内关联,关联出产品名称即可。
参考SQL
未特别说明的情况下,参考SQL为基于MySQL8.0实现。
select max(b.product_name) product_name, sum(a.unit) unit from Orders a left join Products b on a.product_id = b.product_id where a.order_date between '2020-02-01' and '2020-02-29' group by a.product_id having sum(a.unit) >= 100;
本站所有内容均为原创,本站保留所有权利。仅允许非商业用途的转载,但必须注明来源网站、作者、来源链接!否则,由此造成的一切后果,由转载方承担!
干货分享、技术提升、面试笔试、学习交流,欢迎关注公众号:xuesql。QQ学习交流群:209942678。