diff --git a/problem1.sql b/problem1.sql new file mode 100644 index 0000000..7d955ef --- /dev/null +++ b/problem1.sql @@ -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 \ No newline at end of file