Leetcode1511. Customer Order Frequency(简单)

题目
Table: Customers

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| customer_id   | int     |
| name          | varchar |
| country       | varchar |
+---------------+---------+

customer_id is the primary key for this table.
This table contains information of the customers in the company.

Table: Product

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| product_id    | int     |
| description   | varchar |
| price         | int     |
+---------------+---------+

product_id is the primary key for this table.
This table contains information of the products in the company.
price is the product cost.

Table: Orders

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| order_id      | int     |
| customer_id   | int     |
| product_id    | int     |
| order_date    | date    |
| quantity      | int     |
+---------------+---------+

order_id is the primary key for this table.
This table contains information on customer orders.
customer_id is the id of the customer who bought "quantity" products with id "product_id".
Order_date is the date in format ('YYYY-MM-DD') when the order was shipped.

Write an SQL query to report the customer_id and customer_name of customers who have spent at least $100 in each month of June and July 2020.

Return the result table in any order.

The query result format is in the following example.

Customers

+--------------+-----------+-------------+
| customer_id  | name      | country     |
+--------------+-----------+-------------+
| 1            | Winston   | USA         |
| 2            | Jonathan  | Peru        |
| 3            | Moustafa  | Egypt       |
+--------------+-----------+-------------+

Product

+--------------+-------------+-------------+
| product_id   | description | price       |
+--------------+-------------+-------------+
| 10           | LC Phone    | 300         |
| 20           | LC T-Shirt  | 10          |
| 30           | LC Book     | 45          |
| 40           | LC Keychain | 2           |
+--------------+-------------+-------------+

Orders

+--------------+-------------+-------------+-------------+-----------+
| order_id     | customer_id | product_id  | order_date  | quantity  |
+--------------+-------------+-------------+-------------+-----------+
| 1            | 1           | 10          | 2020-06-10  | 1         |
| 2            | 1           | 20          | 2020-07-01  | 1         |
| 3            | 1           | 30          | 2020-07-08  | 2         |
| 4            | 2           | 10          | 2020-06-15  | 2         |
| 5            | 2           | 40          | 2020-07-01  | 10        |
| 6            | 3           | 20          | 2020-06-24  | 2         |
| 7            | 3           | 30          | 2020-06-25  | 2         |
| 9            | 3           | 30          | 2020-05-08  | 3         |
+--------------+-------------+-------------+-------------+-----------+

Result table:

+--------------+------------+
| customer_id  | name       |  
+--------------+------------+
| 1            | Winston    |
+--------------+------------+ 

Winston spent 300 (300 * 1) in June and 100 ( 10 * 1 + 45 * 2) in July 2020.
Jonathan spent 600 (300 * 2) in June and $20 ( 2 * 10) in July 2020.
Moustafa spent 110 (10 * 2 + 45 * 2) in June and 0 in July 2020.

解答
先连接Product和Orders表
再选出2020年 6、7月的记录

select O.customer_id, O.name, dateformat(O.order_date, '%Y-%m') as ym,
O.quanttity*P.price as a
from Product as P
join Orders as O
on P.product_id = O.product_id 
where year(O.order_date) = 2020 and (month(O.order_date) = 6 OR month(O.order_date) = 7);

对id和月份分组 统计总的消费

select distinct tmp.customer_id, tmp.name
from (select O.customer_id, O.name, dateformat(O.order_date, '%Y-%m') as ym,
O.quanttity*P.price as a
from Product as P
join Orders as O
on P.product_id = O.product_id 
where year(O.order_date) = 2020 and (month(O.order_date) = 6 OR month(O.order_date) = 7)) as tmp
order by tmp.customer_id, tmp.ym
having sum(tmp.a) >=100

好像不太对 题目要求的是返回6月和7月都花了至少100美元的人
我做的是6月或7月

三表连接

select o.customer_id, c.name
from Customers c, Product p, Orders o
where c.customer_id = o.customer_id and p.product_id = o.product_id

对id分组 统计 六月 七月的花销

select o.customer_id, c.name,
sum(if(year(O.order_date) = 2020 and month(O.order_date) = 6, O.quantity * P.price, 0)) as june,
sum(if(year(O.order_date) = 2020 and month(O.order_date) = 7, O.quantity * P.price, 0)) as july
from Customers c, Product p, Orders o
where c.customer_id = o.customer_id and p.product_id = o.product_id
group by c.customer_id

然后筛选6月和7月都花了至少100美元

select o.customer_id, c.name
from Customers c, Product p, Orders o
where c.customer_id = o.customer_id and p.product_id = o.product_id
group by c.customer_id
having ,
sum(if(year(O.order_date) = 2020 and month(O.order_date) = 6, O.quantity * P.price, 0)) >=100 and 
sum(if(year(O.order_date) = 2020 and month(O.order_date) = 7, O.quantity * P.price, 0)) >=100

你可能感兴趣的:(Leetcode1511. Customer Order Frequency(简单))