SQL Server provides a powerful set of tools for data manipulation, and one of its most useful features is the ability to unpivot data. Unpivoting allows you to transform data from a wide format to a long format, making it easier to analyze and report on your data. In this article, we'll explore how to use the UNPIVOT function in SQL Server with guidance from W3Schools.

The UNPIVOT function in SQL Server is designed to convert multiple rows from a single column into multiple columns. It's particularly useful when you have data in a format that's difficult to work with, such as when it's stored in a string or a numeric column. By unpivoting your data, you can transform it into a format that's easy to sort, filter, and aggregate.

Understanding Unpivoting in SQL Server
Before we dive into the syntax and usage of the UNPIVOT function, let's first understand what unpivoting is and why it's useful. Unpivoting is a data transformation process that involves converting a dataset from a row-based format to a column-based format.

In other words, it's a way of 'unrolling' your data so that each data point is represented as a separate record. This can make it much easier to analyze and report on your data, as you can apply standard SQL functions and operators to each column individually.
Wide Format vs. Long Format Data

To see how unpivoting can help, let's consider a simple example. Suppose we have a table called 'Sales' with the following wide format data:
ID | Product | Quantity | Price
---+----------+----------+------
1 | A | 10 | 5.00
1 | B | 5 | 3.00
2 | C | 8 | 6.00
2 | D | 3 | 4.00
This table shows the sales for two customers (ID 1 and 2) for four different products (A, B, C, and D). However, this wide format makes it difficult to apply aggregate functions like SUM or AVG to calculate the total sales for each customer.
Transforming Data with UNPIVOT

To make this data easier to analyze, we can use the UNPIVOT function to transform it into a long format. Here's how you might do it:
SELECT ID,
Product,
Quantity,
Price
FROM (SELECT ID, Product, Quantity, Price FROM Sales) AS Source
UNPIVOT
(
[Value]
FOR ProductName IN ([A], [B], [C], [D])
) AS UnPivotResult;
This query takes the original Sales table as its source and unpivots it into a long format. The resulting output looks like this:
- ID: 1, Product: A, Quantity: 10, Price: 5.00
- ID: 1, Product: B, Quantity: 5, Price: 3.00
- ID: 2, Product: C, Quantity: 8, Price: 6.00
- ID: 2, Product: D, Quantity: 3, Price: 4.00

Using UNPIVOT in More Complex Scenarios
While the previous example was relatively simple, the UNPIVOT function can also be used to transform more complex data. For instance, you might have a table that contains sales data for multiple products and multiple categories, like this:




















ID | Product | Category | Quantity | Price
---+---------+----------+----------+------
1 | A | Fruits | 10 | 5.00
1 | B | Vegetables| 5 | 3.00
2 | C | Fruits | 8 | 6.00
2 | D | Meat | 3 | 4.00
In this case, you might want to unpivot the data to separate the 'Category' column into its own row, like this:
- ID: 1, Product: A, Category: Fruits, Quantity: 10, Price: 5.00
- ID: 1, Product: B, Category: Vegetables, Quantity: 5, Price: 3.00
- ID: 2, Product: C, Category: Fruits, Quantity: 8, Price: 6.00
- ID: 2, Product: D, Category: Meat, Quantity: 3, Price: 4.00
To do this, you can use multiple UNPIVOT clauses, like this:
SELECT ID,
Product,
Category.a ProduktCategory,
Quantity,
Price
FROM (SELECT ID, Product, Category, Quantity, Price FROM Sales) AS Source
UNPIVOT (Category FOR CategoryName IN ([Fruits], [Vegetables], [Meat])) AS UnPivotResult;
Unpivoting with PIVOT Tables
In some cases, you might need to unpivot data that's already been pivoted. For instance, you might have a PIVOT table that looks like this:
bage> |ID | Product_A | Product_B | Product_C | Product_D | ---+-----------+-----------+-----------+----------- 1 | 10 | 5 | 0 | 0 | 2 | 0 | 0 | 8 | 3 |
To unpivot this data, you can use the following query:
SELECT ID,
Product,
Quantity
FROM (SELECT ID, [A], [B], [C], [D] FROM PivotTable) AS Source
UNPIVOT (Quantity FOR Product IN ([A], [B], [C], [D])) AS UnPivotResult;
This query takes the original PivotTable as its source and unpivots it into a long format, with each product name in a separate row.
Conclusion: Leveraging UNPIVOT for Data Analysis
The UNPIVOT function in SQL Server is a powerful tool for data transformation, allowing you to convert data from wide format to long format with just a few lines of code. Whether you're working with simple sales data or complex, multi-dimensional data, UNPIVOT can help make your data analysis tasks easier and more efficient.
Remember, the key to effective data analysis is having your data in the right format. With UNPIVOT, you have the power to transform your data and make it work for you, rather than working around its quirks. So, whether you're using W3Schools or any other SQL resource, understanding and using UNPIVOT effectively will greatly enhance your data analysis capabilities.