Advanced Reporting
Unpivoting Data
Senior Data Analyst
March 23, 2026
5 min read
The Problem
Data arrived in a "Wide" format with separate columns for each month. I needed it "Long" for analysis.
The Technique: UNION ALL
SELECT product, 'Jan' AS month, jan_sales AS amount FROM wide_table
UNION ALL
SELECT product, 'Feb' AS month, feb_sales AS amount FROM wide_table
UNION ALL
SELECT product, 'Mar' AS month, mar_sales AS amount FROM wide_table;
PostgreSQL: UNNEST or LATERAL
For cleaner unpivoting, use `LATERAL` with `VALUES`.
*Day 83: Dynamic SQL Basics.*