---
title: "SQL Query to Pivot Data from Rows to Columns in SQL Server"  
description: "SQL Query to Pivot Data from Rows to Columns in SQL Server"  
author: "Anubhav Sharma"  
published: 2024-07-15  
updated: 2024-07-16  
canonical: https://www.mindstick.com/forum/160916/sql-query-to-pivot-data-from-rows-to-columns-in-sql-server  
category: "SQL Server"  
tags: ["database", "sql server", "sql server 2008", "sql server 2012", "sql server 2022"]  
reading_time: 2 minutes  

---

# SQL Query to Pivot Data from Rows to Columns in SQL Server

Hello,

I have a table with `ProductID`, `Month`, and `Sales` columns. I need to pivot this data so that each month becomes a column. How can I do this in SQL Server?

## Replies

### Reply by Ravi Vishwakarma

A PIVOT table in SQL Server is a way to convert data from rows to columns, grouping values ​​as needed. It is particularly useful for collecting and analyzing data in a readable format.

## Syntax-

```javascript
SELECT *
FROM (
   -- Subquery that retrieves the data to pivot
   SELECT <non-pivoted column(s)>,
          <pivot column>,
          <value column>
   FROM <source_table>
) AS SourceTable
PIVOT (
   -- PIVOT function parameters
   <aggregate_function>(<value column>)
   FOR <pivot column> IN (<column1>, <column2>, ... <columnN>)
) AS PivotTable;
```

## Example-

Let's see we have a sample SQL table “**EmployeeDetails**” that contains information about Employees,

```plaintext
use MyCollegeDb

select * from
(
	Select EmpName, Gender, Location from EmployeeDetails
) AS EMP
PIVOT(
	Count(EMP.EmpName) FOR Location IN([China],[India],[Rasia])
)AS [PvtTable]
```

Or **Match in the whole location**

```plaintext
USE MyCollegeDb;

-- First, get a list of distinct locations to include in the PIVOT clause
DECLARE @locations NVARCHAR(MAX);
SELECT @locations = STRING_AGG(QUOTENAME(Location), ',')
FROM (SELECT DISTINCT Location FROM EmployeeDetails) AS Locs;

-- Construct and execute the PIVOT query dynamically
DECLARE @sql NVARCHAR(MAX) = '
SELECT *
FROM
(
    Select EmpName, Gender, Location from EmployeeDetails
) AS EMP
PIVOT (
    COUNT(EmpName)
    FOR Location IN (' + @locations + ')
) AS PvtTable
';
--print @sql; -- See dynamic query
EXEC sp_executesql @sql;
```

## Explanation

- First of all, get all `distinct locations`, and combine the whole locations in one string format
- Second, write the `PIVOT query dynamically` to evaluate it.

## Read more

[**Define the PIVOT Table with examples in the SQL server.**](https://www.mindstick.com/articles/336334/define-the-pivot-table-with-examples-in-sql-server)

[**SQL Query to Get Yearly Aggregated Data in SQL Server**](https://www.mindstick.com/forum/160930/sql-query-to-get-yearly-aggregated-data-in-sql-server)

[**Help with Writing a Query to Combine Multiple Rows into One in SQL Server**](https://www.mindstick.com/forum/160929/help-with-writing-a-query-to-combine-multiple-rows-into-one-in-sql-server)

[**How to Write a Query to Find the Longest String in a Column in SQL Server?**](https://www.mindstick.com/forum/160928/how-to-write-a-query-to-find-the-longest-string-in-a-column-in-sql-server)

[**SQL Query to Get Distinct Values from a Column in SQL Server**](https://www.mindstick.com/forum/160927/sql-query-to-get-distinct-values-from-a-column-in-sql-server)


---

Original Source: https://www.mindstick.com/forum/160916/sql-query-to-pivot-data-from-rows-to-columns-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
