2023年8月30日 星期三

8/30 每日一題(MYSQL 輸出2月份unit總和在100以上的product_name 分組)

 

Table: Products

+------------------+---------+
| Column Name      | Type    |
+------------------+---------+
| product_id       | int     |
| product_name     | varchar |
| product_category | varchar |
+------------------+---------+
product_id is the primary key (column with unique values) for this table.
This table contains data about the company's products.

 

Table: Orders

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| product_id    | int     |
| order_date    | date    |
| unit          | int     |
+---------------+---------+
This table may have duplicate rows.
product_id is a foreign key (reference column) to the Products table.
unit is the number of products ordered in order_date.

 

Write a solution to get the names of products that have at least 100 units ordered in February 2020 and their amount.

Return the result table in any order.

The result format is in the following example.


# Write your MySQL query statement below
SELECT P.product_name  , SUM(O.unit) AS unit    FROM Products AS P
JOIN Orders O on P.product_id  = O.product_id
WHERE O.order_date BETWEEN "2020-02-01" AND "2020-02-29"
GROUP BY product_name
HAVING unit >= 100



----------------------
SELECT product_name,unit FROM Products JOIN (SELECT product_id,sum(unit) AS 'unit' FROM
Orders
WHERE month(order_date) = 2 AND year(order_date) = 2020
GROUP BY product_id
HAVING sum(unit) >=100)T ON Products.product_id = T.product_id

------------------
# Write your MySQL query statement below

select p.product_name,sum(o.unit) as unit from Products p join Orders o 
on p.product_id=o.product_id where year(o.order_date)='2020' and month(o.order_date)='02'
group by p.product_id having sum(o.unit)>=100

標籤:

0 個意見:

張貼留言

訂閱 張貼留言 [Atom]

<< 首頁