Given a Products table that stores product prices for different stores, write a SQL query to find the price of each product in each store.
The Products table has the following structure:
product_id: Integer representing the product identifierstore: Enum with values ('store1', 'store2', 'store3') indicating which store sells the productprice: Integer representing the price of the product at that store
The combination of (product_id, store) is the primary key, meaning each product can have different prices at different stores.
Goal: Transform the data to show each product as a row with columns for each store's price. If a product is not available at a store, the price should be NULL.
Table Schema
| Column Name | Type | Description |
|---|---|---|
product_id
PK
|
int | Product identifier |
store
PK
|
enum | Store name ('store1', 'store2', 'store3') |
price
|
int | Price of product at this store |
Input & Output
| product_id | store | price |
|---|---|---|
| 0 | store1 | 95 |
| 0 | store3 | 105 |
| 0 | store2 | 70 |
| 1 | store1 | 110 |
| product_id | store1 | store2 | store3 |
|---|---|---|---|
| 0 | 95 | 70 | 105 |
| 1 | 110 |
Product 0 is available in all three stores with different prices (95, 70, 105). Product 1 is only available in store1 for 110, so store2 and store3 show NULL values.
| product_id | store | price |
|---|---|---|
| 5 | store2 | 200 |
| product_id | store1 | store2 | store3 |
|---|---|---|---|
| 5 | 200 |
Product 5 is only available at store2 for 200. The other stores (store1 and store3) show NULL since this product is not sold there.
Constraints
-
1 ≤ product_id ≤ 1000 -
1 ≤ price ≤ 3000 -
storeis one of'store1','store2', or'store3' -
Each
(product_id, store)combination appears at most once