跳到主要内容

1327.列出指定时间段内所有的下单产品

序号题目难度
1327题目链接简单

题目大意​

写一个解决方案,要求获取在 2020 年 2 月份下单的数量不少于 100 的产品的名字和数目。

返回结果表单的 顺序无要求 。

表: Products

Column NameType
product_idint
product_namevarchar
product_categoryvarchar

product_id 是该表主键(具有唯一值的列)。 该表包含该公司产品的数据。

表: Orders

Column NameType
product_idint
order_datedate
unitint
该表可能包含重复行。
product_id 是表单 Products 的外键(reference 列)。
unit 是在日期 order_date 内下单产品的数目。

示例​

输入​

Products 表:

product_idproduct_nameproduct_category
1Leetcode SolutionsBook
2Jewels of StringologyBook
3HPLaptop
4LenovoLaptop
5Leetcode KitT-shirt

Orders 表:

product_idorder_dateunit
12020-02-0560
12020-02-1070
22020-01-1830
22020-02-1180
32020-02-172
32020-02-243
42020-03-0120
42020-03-0430
42020-03-0460
52020-02-2550
52020-02-2750
52020-03-0150

输出​

product_nameunit
Leetcode Solutions130
Leetcode Kit100

解释​

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 。

参考答案​

select
t2.product_name
,sum(t1.unit) unit
from Orders t1
left join Products t2
on t1.product_id = t2.product_id
where order_date between '2020-02-01' and '2020-02-29'
group by 1
having unit >= 100
;