Pivoting data in SQL allows transforming row-based data into a column-based format, making it easier to analyze and generate reports. This technique is useful when dealing with categorical data that needs to be summarized across multiple dimensions.
For example, if you have sales data recorded by month, pivoting can help display each month as a separate column instead of multiple rows.
Pivoting is useful for:
Many databases do not have a built-in PIVOT function. Instead, CASE statements with aggregate functions like SUM(), COUNT(), or AVG() can be used.
Suppose there is a sales table with the following structure:
| year | region | total_sales |
|---|---|---|
| 2022 | East | 5000 |
| 2022 | West | 6000 |
| 2023 | East | 7000 |
| 2023 | West | 8000 |
The goal is to pivot this data so that years become columns.
SELECT region,
SUM(CASE WHEN year = 2022 THEN total_sales ELSE 0 END) AS sales_2022,
SUM(CASE WHEN year = 2023 THEN total_sales ELSE 0 END) AS sales_2023
FROM sales
GROUP BY region;| region | sales_2022 | sales_2023 |
|---|---|---|
| East | 5000 | 7000 |
| West | 6000 | 8000 |
This method works on all SQL databases, including MySQL, PostgreSQL, SQLite, and Oracle.
Some databases, like SQL Server, provide a built-in PIVOT function to simplify pivoting.
SELECT * FROM (
SELECT year, region, total_sales FROM sales
) AS SourceTable
PIVOT (
SUM(total_sales) FOR year IN ([2022], [2023])
) AS PivotTable;| region | 2022 | 2023 |
|---|---|---|
| East | 5000 | 7000 |
| West | 6000 | 8000 |
The PIVOT function simplifies queries but is only available in SQL Server.
If the column values (years, categories, etc.) are unknown beforehand, dynamic pivoting is required. This requires dynamic SQL or JSON functions, depending on the database.
SET @sql = NULL;
SELECT GROUP_CONCAT(DISTINCT CONCAT(
'SUM(CASE WHEN year = ', year, ' THEN total_sales ELSE 0 END) AS sales_', year
)) INTO @sql FROM sales;
SET @sql = CONCAT('SELECT region, ', @sql, ' FROM sales GROUP BY region;');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;This approach is flexible and works for any number of years dynamically.
Consider a sales table with revenue categorized by month:
| month | category | revenue |
|---|---|---|
| Jan | Electronics | 10000 |
| Feb | Electronics | 12000 |
| Jan | Clothing | 8000 |
| Feb | Clothing | 9000 |
The goal is to pivot months into columns for better readability.
SELECT category,
SUM(CASE WHEN month = 'Jan' THEN revenue ELSE 0 END) AS Jan_Revenue,
SUM(CASE WHEN month = 'Feb' THEN revenue ELSE 0 END) AS Feb_Revenue
FROM sales
GROUP BY category;| category | Jan_Revenue | Feb_Revenue |
|---|---|---|
| Electronics | 10000 | 12000 |
| Clothing | 8000 | 9000 |
This format makes sales data easier to compare across months.
| Feature | Normal Query (Rows) | Pivot Query (Columns) |
|---|---|---|
| Data format | Vertical (many rows) | Horizontal (fewer rows) |
| Readability | Harder to compare | Easier to compare |
| Reporting | Requires additional processing | Ready for reports |
Pivoting is useful when comparing multiple categories across a time period, such as sales, performance, and expenses.
Pivoting in SQL transforms row-based data into column-based summaries, making reports easier to read.
CASE + Aggregation works in all SQL databases.PIVOT function is available in SQL Server for a simpler approach.Choosing the right method depends on database support and reporting needs.
Sign in to join the discussion and post comments.
Sign in