OkBublewrap

1070. Product Sales Analysis III 본문

개발/SQL

1070. Product Sales Analysis III

옥뽁뽁 2025. 1. 22. 01:25

1070. Product Sales Analysis III

문제

  1. 모든 제품이 판매된 첫 번째 연도를 기준으로 product_id , year, quantity, price
  2. any order

입력 테이블

  1. Sales
    • sale_id
    • product_id
    • year
    • quantity
    • price
  2. Product
    • product_id
    • product_name

풀이

1. product_id별 최소 연도 구하기

product_idyear 최소연도 구하기

select product_id, min(year) as first_year
from Sales
group by product_id

2. 조건절 사용

product_idyear와 맞는 데이터 가져오기

select product_id, year as first_year, quantity, price
from Sales
where (product_id, year) in (
select product_id, min(year) as first_year
from Sales
group by product_id)

'개발 > SQL' 카테고리의 다른 글

1164. Product Price at a Given Date  (0) 2025.01.24
180. Consecutive Numbers  (0) 2025.01.22
1934. Confirmation Rate  (0) 2025.01.21
1280. Students and Examinations  (0) 2025.01.20
기계당 평균 처리 시간  (0) 2025.01.17