[LeetCode/SQL] 1070. Product Sales Analysis III

Sooyeon·2024년 1월 13일
0

문제풀이 

목록 보기
82/95
post-thumbnail

[LeetCode/SQL]


📌 1070. Product Sales Analysis III

문제

풀이

상품 별 첫 판매된 년도,양,가격을 출력하라 

방법 I

SELECT product_id
       ,year AS first_year
       ,quantity
       ,price
FROM Sales
WHERE (product_id,year) IN ( SELECT product_id
                                    ,MIN(year) AS year
                             FROM Sales
                             GROUP BY product_id
)

방법 II

WITH min_year_table AS (
SELECT product_id, min(year) min_year
FROM sales
GROUP BY product_id)

SELECT s.product_id, s.year first_year, quantity, price
FROM sales s
JOIN min_year_table m ON s.product_id = m.product_id and s.year = m.min_year

0개의 댓글