Study - Problems(IT)/LeetCode - SQL

1070. Product Sales Analysis 3

Dev.D 2024. 11. 22. 22:31

* Problem

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.


* Solution (Success)

# Write your MySQL query statement below
with ProductFirstYear as(
    select product_id, min(year) as year
    from Sales
    Group by 1
)
select Sales.product_id, 
    ProductFirstYear.year as  first_year,
    Sales.quantity,
    Sales.price
    from Sales
    inner join ProductFirstYear
    using (product_id, year)