+-------------+-------+
|Column Name |Type|+-------------+-------+
| sale_id | int || product_id | int ||year| int || quantity | int || price | int |+-------------+-------+
(sale_id, year) is the primarykey (combination of columns withuniquevalues) of this table.
product_id is a foreignkey (reference column) to Product table.
Eachrowof this table shows a sale on the product product_id in a certain year.
Note that the price is per unit.
Table: Product
1
2
3
4
5
6
7
8
+--------------+---------+
|Column Name |Type|+--------------+---------+
| product_id | int || product_name | varchar |+--------------+---------+
product_id is the primarykey (columnwithuniquevalues) of this table.
Eachrowof this table indicates the product name ofeach product.
Write a solution to select the product id , year , quantity , and price for the first year of every product sold.
SELECT s.product_id, s.yearAS first_year, s.quantity, s.price
FROM Sales s
JOIN (
SELECT product_id, MIN(year) AS first_year
FROM Sales
GROUPBY product_id
) t
ON s.product_id = t.product_id AND s.year= t.first_year;
1
2
3
4
5
6
7
8
SELECT s.product_id, s.yearAS first_year, s.quantity, s.price
FROM Sales s
JOIN (
SELECT product_id, MIN(year) AS first_year
FROM Sales
GROUPBY product_id
) t
ON s.product_id = t.product_id AND s.year= t.first_year;
1
2
3
4
5
6
7
# Assume Sales is a pandas DataFrameimport pandas as pd
defproduct_sales_analysis_iii(Sales: pd.DataFrame) -> pd.DataFrame:
first_year = Sales.groupby('product_id')['year'].min().reset_index(name='first_year')
merged = Sales.merge(first_year, left_on=['product_id', 'year'], right_on=['product_id', 'first_year'])
result = merged[['product_id', 'first_year', 'quantity', 'price']]
return result