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 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
Dynamic Unpivoting TransformationOriginal Pivot TableID | Amazon | Walmart | Target1 | 120 | 100 | 1102 | 80 | NULL | 853 | NULL | 75 | NULLDynamic Query Builder1. Discover columns from schema2. Build UNION for each store3. Filter out NULL values4. Execute with prepared stmtNormalized ResultID | Store | Price1 | Amazon | 1201 | Walmart | 1001 | Target | 1102 | Amazon | 802 | Target | 853 | Walmart | 75Generated SQL Query ExampleSELECT product_id, 'Amazon' as store, Amazon as price FROM Products WHERE Amazon IS NOT NULLUNION ALLSELECT product_id, 'Walmart' as store, Walmart as price FROM Products WHERE Walmart IS NOT NULLUNION ALLSELECT product_id, 'Target' as store, Target as price FROM Products WHERE Target IS NOT NULL;๐ŸŽฏ Key Benefits: Fully Dynamic โ€ข Zero Maintenance โ€ข Handles Any Store Names
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.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
58.0K Views
Medium Frequency
~35 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen