Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions problem1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
1. We need to find all sales that occurred in the first year each product was sold
2. We first compute the first year that a product was sold.
3. Then we join it with the sales table on product id and year to fetch all the first year sales
"""


with cte1 as
(
select product_id, min(year) as year
from sales
group by product_id
)

select c.product_id, c.year as first_year, quantity, price
from cte1 c
join sales s on c.product_id = s.product_id and c.year = s.year