[LeetCode] Sales Analysis III

아르당·2026년 3월 31일

LeetCode

목록 보기
234/263
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

Table: Product

Column NameType
product_idint
product_namevarchar
unit_priceint

product_id는 이 테이블의 기본 키이다.
테이블의 각 행은 각 제품의 이름과 가격을 나타낸다.

Table: Sales

Column NameType
seller_idint
product_idint
buyer_idint
sale_datedate
quantityint
priceint

이 테이블은 복제한 행을 가질 수 있다.
product는 Product 테이블의 외래키이다.
테이블의 각 행은 하나의 판매에 대한 어떤 정보를 포함한다.

2019년 1분기에 판매된 제품을 보고하는 방법을 작성해라.

Example

Input:
Product table:

product_idproduct_nameunit_price
1S81000
2G4800
3iPhone1400

Sales table:

seller_idproduct_idbuyer_idsale_datequantityprice
1112019-01-2122000
1222019-02-171800
2232019-06-021800
3342019-05-1322800

Output:

product_idproduct_name
1S8

Explanation:
제품 1은 2019 봄에만 팔렸다.
제품 2는 2019 봄에 팔렸지만 2019 봄 이후에도 팔렸다.
제품 3은 2019 봄 이후에 팔렸다.
오직 2019 봄에만 팔린 제품 1만 반환한다.

Solved

-- 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'
profile
내 마음대로 코드 작성하는 세상

0개의 댓글