Pivoting and unpivoting are techniques in SQL that allow you to transform data between column-based and row-based formats. Pivoting helps to aggregate and display data in a more readable and concise way by converting rows into columns. On the other hand, unpivoting helps you reverse this process, turning columns into rows for further analysis. In this article, we will explore both techniques with examples.
Pivoting is the process of turning rows into columns. This technique is useful when you want to summarize data in a compact form, especially when dealing with aggregate data.
Suppose you have a table sales
with the following data:
sales +----------+--------+-------+--------+ | month_id | region | sales | profit | +----------+--------+-------+--------+ | 1 | East | 500 | 100 | | 1 | West | 600 | 120 | | 2 | East | 700 | 140 | | 2 | West | 800 | 160 | +----------+--------+-------+--------+
We want to pivot this data so that each region appears as a separate column, and the sales values for each month are shown in those columns. The following SQL query uses the PIVOT
operator:
SELECT month_id, [East] AS East_Sales, [West] AS West_Sales FROM (SELECT month_id, region, sales FROM sales) AS SourceTable PIVOT (SUM(sales) FOR region IN ([East], [West])) AS PivotTable;
In this query:
SELECT month_id, region, sales
retrieves the data.PIVOT
operator transforms rows into columns based on the region
column.SUM(sales)
function aggregates the sales data for each month and region.The result of this pivot query will look like this:
+----------+------------+------------+ | month_id | East_Sales | West_Sales | +----------+------------+------------+ | 1 | 500 | 600 | | 2 | 700 | 800 | +----------+------------+------------+
Unpivoting is the reverse of pivoting; it converts columns into rows. This technique is helpful when you need to normalize data or prepare it for further analysis.
Let’s say you have a table monthly_sales
that contains data for East and West regions as columns, and you want to unpivot the data to make it more granular:
monthly_sales +----------+------------+------------+ | month_id | East_Sales | West_Sales | +----------+------------+------------+ | 1 | 500 | 600 | | 2 | 700 | 800 | +----------+------------+------------+
To unpivot this data, you can use the UNPIVOT
operator as follows:
SELECT month_id, region, sales FROM (SELECT month_id, East_Sales, West_Sales FROM monthly_sales) AS SourceTable UNPIVOT (sales FOR region IN (East_Sales, West_Sales)) AS UnpivotTable;
In this query:
SELECT month_id, East_Sales, West_Sales
retrieves the data.UNPIVOT
operator turns the columns East_Sales
and West_Sales
into rows under the region
column.sales
column contains the values from East_Sales
and West_Sales
after unpivoting.The result of the unpivot query will look like this:
+----------+--------+-------+ | month_id | region | sales | +----------+--------+-------+ | 1 | East | 500 | | 1 | West | 600 | | 2 | East | 700 | | 2 | West | 800 | +----------+--------+-------+
Pivoting and unpivoting are powerful techniques for reshaping data in SQL, but knowing when to use them is important:
Pivoting and unpivoting are essential techniques for reshaping data in SQL. Pivoting allows you to convert rows into columns for better summarization, while unpivoting helps transform columns into rows for more granular analysis. Understanding when and how to use these techniques will help you manipulate your data effectively and produce the desired results in your reports or analysis.