+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_id | int |
| store | enum |
| price | int |
+-------------+---------+
In SQL, (product_id, store) is the primary key for this table.
store is a category of type ('store1', 'store2', 'store3') where each represents the store this product is available at.
price is the price of the product at this store.
SELECT product_id,
MAX(CASEWHEN store ='store1'THEN price END) AS store1,
MAX(CASEWHEN store ='store2'THEN price END) AS store2,
MAX(CASEWHEN store ='store3'THEN price END) AS store3
FROM Products
GROUPBY product_id;
1
2
3
4
5
6
SELECT product_id,
MAX(CASEWHEN store ='store1'THEN price END) AS store1,
MAX(CASEWHEN store ='store2'THEN price END) AS store2,
MAX(CASEWHEN store ='store3'THEN price END) AS store3
FROM Products
GROUPBY product_id;
1
2
3
4
5
6
7
8
9
10
# Assume Products is a pandas DataFrameimport pandas as pd
defproducts_price_for_each_store(Products: pd.DataFrame) -> pd.DataFrame:
result = Products.pivot(index='product_id', columns='store', values='price').reset_index()
# Optional: ensure columns are in the right orderfor col in ['store1', 'store2', 'store3']:
if col notin result.columns:
result[col] =None result = result[['product_id', 'store1', 'store2', 'store3']]
return result