Dynamic Unpivoting of a Table - Problem
Transform a Product Pricing Table - The Ultimate SQL Unpivoting Challenge!
You're given a dynamic pivot table containing product prices across multiple stores. Your mission: transform this wide table into a normalized format that's much easier to work with!
The Challenge:
โข Input: A table with
โข Each store column contains the product's price in that store (or NULL if not available)
โข The number and names of store columns vary between test cases
โข Output: A clean table with exactly 3 columns:
๐ฏ Goal: Create a stored procedure
Why This Matters: This is a real-world scenario where data comes in pivot format but you need it normalized for analysis, reporting, or further processing. Master this and you'll handle any data transformation challenge!
You're given a dynamic pivot table containing product prices across multiple stores. Your mission: transform this wide table into a normalized format that's much easier to work with!
The Challenge:
โข Input: A table with
product_id and multiple store columns (store_name1, store_name2, ..., store_namen)โข Each store column contains the product's price in that store (or NULL if not available)
โข The number and names of store columns vary between test cases
โข Output: A clean table with exactly 3 columns:
product_id, store, and price๐ฏ Goal: Create a stored procedure
UnpivotProducts that dynamically unpivots any table structure and excludes NULL prices.Why This Matters: This is a real-world scenario where data comes in pivot format but you need it normalized for analysis, reporting, or further processing. Master this and you'll handle any data transformation challenge!
Input & Output
Basic Example - Multiple Stores
$
Input:
Products table:
+------------+--------+--------+--------+
| product_id | LC_Store| Nozama | Shop |
+------------+--------+--------+--------+
| 0 | 95 | 100 | 105 |
| 1 | 70 | NULL | 80 |
+------------+--------+--------+--------+
โบ
Output:
+------------+----------+-------+
| product_id | store | price |
+------------+----------+-------+
| 0 | LC_Store | 95 |
| 0 | Nozama | 100 |
| 0 | Shop | 105 |
| 1 | LC_Store | 70 |
| 1 | Shop | 80 |
+------------+----------+-------+
๐ก Note:
Product 0 is available in all 3 stores, so we get 3 rows. Product 1 is only available in LC_Store and Shop (Nozama price is NULL), so we get 2 rows. Notice how NULL values are excluded from the result.
Edge Case - Single Store
$
Input:
Products table:
+------------+---------+
| product_id | Store1 |
+------------+---------+
| 0 | 10 |
| 1 | 25 |
+------------+---------+
โบ
Output:
+------------+--------+-------+
| product_id | store | price |
+------------+--------+-------+
| 0 | Store1 | 10 |
| 1 | Store1 | 25 |
+------------+--------+-------+
๐ก Note:
Even with just one store column, the unpivoting process works correctly. Each product gets one row with the store name and price.
Edge Case - All NULL Prices
$
Input:
Products table:
+------------+--------+--------+
| product_id | Store1 | Store2 |
+------------+--------+--------+
| 0 | NULL | NULL |
| 1 | 50 | NULL |
+------------+--------+--------+
โบ
Output:
+------------+--------+-------+
| product_id | store | price |
+------------+--------+-------+
| 1 | Store1 | 50 |
+------------+--------+-------+
๐ก Note:
Product 0 has NULL prices in all stores, so it doesn't appear in the result. Product 1 only has a price in Store1, so we get exactly one row. This demonstrates proper NULL handling.
Constraints
- 1 โค Number of products โค 1000
- 1 โค Number of stores โค 30
- Store names can be any valid column name
- Product prices are positive integers or NULL
- product_id is always the primary key column
Visualization
Tap to expand
Understanding the Visualization
1
Wide Format Input
Products table with multiple store columns (pivot format)
2
Column Discovery
INFORMATION_SCHEMA identifies all store columns dynamically
3
Query Construction
Build UNION ALL query for each store column with NULL filtering
4
Execution
Execute dynamic SQL using prepared statements
5
Normalized Output
Clean table with product_id, store, and price columns
Key Takeaway
๐ฏ Key Insight: By leveraging SQL metadata and dynamic query construction, we can create a truly flexible unpivoting solution that adapts to any table structure automatically.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code