Data Analyst/daily

[데이터분석] 코드카타 101

rubus0304 2024. 11. 14. 10:02

101. Product Sales Analysis III

https://leetcode.com/problems/product-sales-analysis-iii/description/

 

Write a solution to select the product id, year, quantity, and price for the first year of every product sold.

Return the resulting table in any order.

 

(오답)  첫 해에 판매해서 min 썼고, left join 했는데 왜 안 될까.

select b.product_id,
          min(year) first_year,
          quantity,
          price
from Sales a left join product b on a.product_id = b.product_id
group by 1

 

 

(정답) 이렇게 따로 where절로 빼서 product_id,와 year을 지정해주는 거랑 그냥 원래 select 문에서 지정하는거랑 뭐가다른거지

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