문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
Table: Product
| Column Name | Type |
|---|---|
| product_id | int |
| product_name | varchar |
| unit_price | int |
product_id는 이 테이블의 기본 키이다.
테이블의 각 행은 각 제품의 이름과 가격을 나타낸다.
Table: Sales
| Column Name | Type |
|---|---|
| seller_id | int |
| product_id | int |
| buyer_id | int |
| sale_date | date |
| quantity | int |
| price | int |
이 테이블은 복제한 행을 가질 수 있다.
product는 Product 테이블의 외래키이다.
테이블의 각 행은 하나의 판매에 대한 어떤 정보를 포함한다.
2019년 1분기에 판매된 제품을 보고하는 방법을 작성해라.
Input:
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 |
Output:
| product_id | product_name |
|---|---|
| 1 | S8 |
Explanation:
제품 1은 2019 봄에만 팔렸다.
제품 2는 2019 봄에 팔렸지만 2019 봄 이후에도 팔렸다.
제품 3은 2019 봄 이후에 팔렸다.
오직 2019 봄에만 팔린 제품 1만 반환한다.
-- Write your PostgreSQL query statement below
select A.product_id, A.product_name
from Product A
join Sales B on A.product_id = B.product_id
group by A.product_id, A.product_name
having min(b.sale_date) >= '2019-01-01' and max(b.sale_date) <= '2019-03-31'