A pivot table is an Excel reporting tool that offers a summarized view of data that has been categorized into groups. While Microsoft Excel has built-in pivot table functionality, SQL Server too allows you to create pivot tables, bringing the advantage of querying and manipulating data in a tabular and more informative way. This article will guide you on how to create and manipulate SQL Server pivot tables in SQL Server Management Studio (SSMS).

Using SQL Server pivot tables can help in filtering and aggregating data, making complex data analysis more accessible and discoverable.

Creating a SQL Server Pivot Table
To create a SQL Server pivot table, you first need to pivot your data using the PIVOT operator in SQL Server. Then, you can easily export the result to an Excel file and apply a pivot table format to it.

Below are the steps to follow:
Step 1: Pivoting Data using SQL Server

Before creating the pivot table, you need to pivot your data using the PIVOT operator which transforms the data from a row format to a column format. Here is an example of how to do it:
Example: If you have the data below in a table named "Sales",
id | city | year | sales
---+------+------+------
1 | New York | 2020 | 1000
1 | New York | 2021 | 1200
2 | Sydney | 2020 | 1100
2 | Sydney | 2021 | 1300

The following query will pivot the data:
SELECT city, sum(CASE WHEN year = 2020 THEN sales ELSE NULL END) AS '2020',
sum(CASE WHEN year = 2021 THEN sales ELSE NULL END) AS '2021'
FROM Sales
GROUP BY city
Step 2: exporting to Excel and creating the pivot table

After pivoting the data in SQL Server, you can export the result to an Excel file by right-clicking on the result set in the 'Results' tab and selecting 'Export Data With Advanced...', then select 'Excel' as the destination.
The output will be a tabular data, and you can then apply a pivot table format using the Excel PivotTable feature by going to 'Insert' in the Excel menu and selecting 'PivotTable' and 'PivotChart'.




















Manipulating SQL Server Pivot Tables
You can filter, sort, and sum data in a SQL Server pivot table to provide a more detailed and focused report on your data.
Here are some manipulations you can perform:
Filtering Data in a SQL Server Pivot Table
You can filter data in a pivot table by clicking on the options in the filter fields located in the column header. This enables you to view specific data of interest.
Sorting Data in a SQL Server Pivot Table
SQL Server allows you to sort data in a pivot table based on specific columns. This helps to organize the data and make it more readable.
Summarizing Data in a SQL Server Pivot Table
The default calculation for values in a pivot table is summation. However, you can change this to other calculations like MAX, MIN, and AVERAGE using the Excel SUM, MAX, MIN, or AVERAGE functions.
SQL Server's pivot tables provide a powerful tool for filtering, aggregating, and sorting large amounts of data, transforming it into a more informative and easier-to-read format. Happy pivoting!