Problems / Unpivot Columns to Rows / Editorial
This is the inverse of pivoting — converting columns back into rows (also called unpivot or melt).
Write one SELECT per column you want to unpivot, each producing:
product_name
'Q1'
'Q2'
sales_amount
Then UNION ALL the results together.
Each SELECT reads the same table but extracts a different column. UNION ALL stacks them vertically. The result is N × M rows where N is the original row count and M is the number of unpivoted columns.
N × M
DuckDB supports UNPIVOT syntax natively:
UNPIVOT
```sql
UNPIVOT quarterly_results
ON q1_sales, q2_sales, q3_sales, q4_sales
INTO NAME quarter VALUE sales_amount
```
But UNION ALL is more portable across SQL dialects.
Solve Unpivot Columns to Rows yourself →