1+ -- 1327. List the Products Ordered in a Period
2+ --
3+ -- Table: Products
4+ --
5+ -- +------------------+---------+
6+ -- | Column Name | Type |
7+ -- +------------------+---------+
8+ -- | product_id | int |
9+ -- | product_name | varchar |
10+ -- | product_category | varchar |
11+ -- +------------------+---------+
12+ -- product_id is the primary key for this table.
13+ -- This table contains data about the company's products.
14+ -- Table: Orders
15+ --
16+ -- +---------------+---------+
17+ -- | Column Name | Type |
18+ -- +---------------+---------+
19+ -- | product_id | int |
20+ -- | order_date | date |
21+ -- | unit | int |
22+ -- +---------------+---------+
23+ -- There is no primary key for this table. It may have duplicate rows.
24+ -- product_id is a foreign key to Products table.
25+ -- unit is the number of products ordered in order_date.
26+ --
27+ --
28+ -- Write an SQL query to get the names of products with greater than or equal to 100 units ordered in February 2020 and their amount.
29+ --
30+ -- Return result table in any order.
31+ --
32+ -- The query result format is in the following example:
33+ --
34+ --
35+ --
36+ -- Products table:
37+ -- +-------------+-----------------------+------------------+
38+ -- | product_id | product_name | product_category |
39+ -- +-------------+-----------------------+------------------+
40+ -- | 1 | Leetcode Solutions | Book |
41+ -- | 2 | Jewels of Stringology | Book |
42+ -- | 3 | HP | Laptop |
43+ -- | 4 | Lenovo | Laptop |
44+ -- | 5 | Leetcode Kit | T-shirt |
45+ -- +-------------+-----------------------+------------------+
46+ --
47+ -- Orders table:
48+ -- +--------------+--------------+----------+
49+ -- | product_id | order_date | unit |
50+ -- +--------------+--------------+----------+
51+ -- | 1 | 2020-02-05 | 60 |
52+ -- | 1 | 2020-02-10 | 70 |
53+ -- | 2 | 2020-01-18 | 30 |
54+ -- | 2 | 2020-02-11 | 80 |
55+ -- | 3 | 2020-02-17 | 2 |
56+ -- | 3 | 2020-02-24 | 3 |
57+ -- | 4 | 2020-03-01 | 20 |
58+ -- | 4 | 2020-03-04 | 30 |
59+ -- | 4 | 2020-03-04 | 60 |
60+ -- | 5 | 2020-02-25 | 50 |
61+ -- | 5 | 2020-02-27 | 50 |
62+ -- | 5 | 2020-03-01 | 50 |
63+ -- +--------------+--------------+----------+
64+ --
65+ -- Result table:
66+ -- +--------------------+---------+
67+ -- | product_name | unit |
68+ -- +--------------------+---------+
69+ -- | Leetcode Solutions | 130 |
70+ -- | Leetcode Kit | 100 |
71+ -- +--------------------+---------+
72+ --
73+ -- Products with product_id = 1 is ordered in February a total of (60 + 70) = 130.
74+ -- Products with product_id = 2 is ordered in February a total of 80.
75+ -- Products with product_id = 3 is ordered in February a total of (2 + 3) = 5.
76+ -- Products with product_id = 4 was not ordered in February 2020.
77+ -- Products with product_id = 5 is ordered in February a total of (50 + 50) = 100.
78+
79+ -- # Write your MySQL query statement below
80+ -- credit: https://leetcode.com/problems/list-the-products-ordered-in-a-period/discuss/491314/MYSQL
81+
82+ select a .product_name , sum (unit) as unit
83+ from Products a
84+ left join Orders b
85+ on a .product_id = b .product_id
86+ where b .order_date between ' 2020-02-01' and ' 2020-02-29'
87+ group by a .product_id
88+ having sum (unit) >= 100
0 commit comments