You are given a Products table with product information stored across multiple store columns. Your task is to rearrange the table to normalize the data structure.
Table: Products
| Column Name | Type |
|---|---|
| product_id | int |
| store1 | int |
| store2 | int |
| store3 | int |
product_idis the primary key for this table- Each row shows a product's price in 3 different stores
- If a product is not available in a store, the price will be
NULL
Goal: Transform the table so each row contains (product_id, store, price). Exclude rows where the product is not available in that store (price is NULL).
Table Schema
| Column Name | Type | Description |
|---|---|---|
product_id
PK
|
int | Primary key, unique product identifier |
store1
|
int | Product price in store1, NULL if not available |
store2
|
int | Product price in store2, NULL if not available |
store3
|
int | Product price in store3, NULL if not available |
Input & Output
| product_id | store1 | store2 | store3 |
|---|---|---|---|
| 0 | 95 | 100 | |
| 1 | 110 | 115 |
| product_id | store | price |
|---|---|---|
| 0 | store1 | 95 |
| 0 | store2 | 100 |
| 1 | store2 | 110 |
| 1 | store3 | 115 |
Product 0 is available in store1 (95) and store2 (100) but not store3 (NULL). Product 1 is available in store2 (110) and store3 (115) but not store1 (NULL). The result excludes the NULL entries and creates separate rows for each product-store combination.
| product_id | store1 | store2 | store3 |
|---|---|---|---|
| 2 | 50 | 55 | 60 |
| product_id | store | price |
|---|---|---|
| 2 | store1 | 50 |
| 2 | store2 | 55 |
| 2 | store3 | 60 |
Product 2 is available in all three stores with different prices. Each store gets its own row in the output, creating three rows from one input row.
| product_id | store1 | store2 | store3 |
|---|---|---|---|
| 3 | 200 |
| product_id | store | price |
|---|---|---|
| 3 | store3 | 200 |
Product 3 is only available in store3 with price 200. The NULL values for store1 and store2 are excluded, resulting in only one output row.
Constraints
-
1 ≤ product_id ≤ 1000 - Store prices are positive integers when not NULL
- Each product appears exactly once in the input table