leetcode1084. Sales Analysis III (SQL)

Table: Product

+ --------- + -------------- +
| Column the Name | Type |
+ -------------- + - + -------
| product_id | int |
| PRODUCT_NAME | VARCHAR |
| unit_price | int |
+ -------------- + --------- +
product_id this is the table's primary key
table: Sales

+ ------------- + --------- +
| Column the Name | Type |
+ ------------- + ---- + -----
| seller_id | int |
| product_id | int |
| buyer_id | int |
| sale_date | DATE |
| the Quantity | int |
|. price | int |
+ ------ + ------ --------- +
the table does not have a primary key, it can have duplicate rows.
the product_id Product table is a foreign key.
 

Write a SQL query, report in spring 2019 only sold products. 2019-01-01 to 2019-03-31 sold i.e. between (including) the only product.

Results shown in the following format:

Product table:
+------------+--------------+------------+
| product_id | product_name | unit_price |
+------------+--------------+------------+
| 1          | S8           | 1000       |
| 2          | G4           | 800        |
| 3          | iPhone       | 1400       |
+------------+--------------+------------+

Sales table:
+-----------+------------+----------+------------+----------+-------+
| seller_id | product_id | buyer_id | sale_date  | quantity | price |
+-----------+------------+----------+------------+----------+-------+
| 1         | 1          | 1        | 2019-01-21 | 2        | 2000  |
| 1         | 2          | 2        | 2019-02-17 | 1        | 800   |
| 2         | 2          | 3        | 2019-06-02 | 1        | 800   |
| 3         | 3          | 4        | 2019-05-13 | 2        | 2800  |
+-----------+------------+----------+------------+----------+-------+

The Table the Result:
+ ------------- + -------------- +
| product_id | PRODUCT_NAME |
+ ----------- - + -------------- +
| 1 | S8 |
+ ------------ + ------------- - +
the above mentioned id is only 1 product sold in the spring of 2019, after two other product sales.

Ideas: Find out the list does not meet the requirements, you can determine whether each item in the list.

select product_id,product_name
from Product 
where product_id not in (select product_id from Sales where sale_date>'2019-03-31' or sale_date<'2019-01-01');

 

Published 611 original articles · won praise 10000 + · views 1.43 million +

Guess you like

Origin blog.csdn.net/hebtu666/article/details/104437621