What are Pivot And Unpivot In SQL Server
Loading
What are Pivot And Unpivot In SQL Server
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Tuhin PaulPosted Feb 6, 2025, 7:30 PM
What is Pivot in SQL Server?
Pivot is a SQL Server operator that rotates data from rows to columns. It's used to transform data from a state of rows to columns, making it easier to analyze and report.
What is Unpivot in SQL Server?
Unpivot is the opposite of Pivot. It's a SQL Server operator that rotates data from columns to rows. It's used to transform data from a state of columns to rows, making it easier to analyze and report.
Scenario:
Suppose you have a table
Salesthat stores quarterly sales data for products:1. Pivot Example
You want to transform the data so that each quarter becomes a column, and the sales amounts are aggregated for each product.
Output:
Tharunkumar MagudeeswaranPosted Feb 7, 2025, 5:34 PM
Pivot is converting rows into columns.
Unpivot is opposite of pivot which converts column into rows.
Tuhin PaulPosted Feb 6, 2025, 7:33 PM
Part -2
2. Unpivot Example
Now, suppose you have a pivoted table
PivotedSales:You want to transform it back into the original format.
Output:
Muhammad Imran AnsariPosted Feb 6, 2025, 5:00 PM
In SQL Server, PIVOT and UNPIVOT are relational operators used to transform data between rows and columns, making it easier to analyze and present data in a more structured format.
PIVOT:
The PIVOT operator converts row-level data into a columnar format. It is typically used to aggregate data and rotate rows into columns, making it useful for creating summary tables or cross-tab reports.
Example
UNPIVOT:
The UNPIVOT operator does the opposite of PIVOT. It converts columnar data back into rows. This is useful when you need to normalize data or restructure it for further analysis.
Example
Key Points:
PIVOT is used to aggregate and rotate rows into columns.
UNPIVOT is used to convert columns back into rows.
Both operations are useful for reshaping data to meet specific reporting or analysis needs.
Sreenath KappoorPosted Feb 6, 2025, 6:04 AM
PIVOT and UNPIVOT are powerful operations used to transform data and make it more readable, efficient, and manageable.
These operations allow us to manipulate tables by switching between rows and columns, which can be crucial for summarizing data, reporting, and data analysis.
Understanding how to use PIVOT and UNPIVOT operators effectively can significantly enhance our data manipulation capabilities.
Sample table:
CREATE TABLE ProductSales (
Product NVARCHAR(50),
Year INT,
Sales INT
);
INSERT INTO ProductSales (Product, Year, Sales)
VALUES
('Product1', 2020, 10000),
('Product1', 2021, 15000),
('Product2', 2020, 20000),
('Product2', 2021, 25000);
SELECT * FROM ProductSales;
The 'PIVOT' operator enables you to rotate your data from a state of rows to a state of columns. This is particularly useful when you want to generate a summary report from your data.
To generate a summary report that shows the sales of each product by year, you can use the 'PIVOT' operator as follows:
SELECT *
FROM (
SELECT Product, Year, Sales
FROM ProductSales
) AS SourceTable
PIVOT (
SUM(Sales)
FOR Year IN ([2020], [2021])
) AS PivotTable;
UNPIVOT allows you to convert your columns into rows. This is particularly useful when you need to normalize your data.
You can use the UNPIVOT operator to convert the pivoted data back to the original table format
-- Step 1: Pivot the data and store it in a temporary table
SELECT * INTO #PivotedTable
FROM (
SELECT Product, Year, Sales
FROM ProductSales
) AS SourceTable
PIVOT (
SUM(Sales)
FOR Year IN ([2020], [2021])
) AS PivotTable;
-- Step 2: Unpivot the data back to its original format
SELECT Product, Year, Sales
FROM #PivotedTable
UNPIVOT (
Sales FOR Year IN ([2020], [2021])
) AS UnpivotedTable;
-- Step 3: Drop the temporary table after use
DROP TABLE #PivotedTable;