In this post explains how to use the PIVOT clause in SQL Server (Transact-SQL) with syntax and examples.
The SQL Server (Transact-SQL) PIVOT clause allows you to write a cross-tabulation. This means that you can aggregate your results and rotate rows into columns.
The syntax for the PIVOT clause in SQL Server (Transact-SQL) is:
A column or expression that will display as the first column in the pivot table.
The column heading for the first column in the pivot table.
A list of values to pivot.
A SELECT statement that provides the source data for the pivot table.
An alias for source_table.
An aggregate function such as SUM, COUNT, MIN, MAX, or AVG.
The column or expression that will be used with the aggregate_function.
The column that contains the pivot values.
An alias for the pivot table.
The PIVOT clause can be used in the following versions of SQL Server (Transact-SQL):
If you want to follow along with this tutorial, get the DDL to create the tables and the DML to populate the data. Then try the examples in your own database!
The PIVOT clause can be used in SQL Server (Transact-SQL).
Let's look at an example. If we had an employees table that contained the following data:
employee_number | last_name | first_name | salary | dept_id |
---|---|---|---|---|
12009 | Sutherland | Barbara | 54000 | 45 |
34974 | Yates | Fred | 80000 | 45 |
34987 | Erickson | Neil | 42000 | 45 |
45001 | Parker | Sally | 57500 | 30 |
75623 | Gates | Steve | 65000 | 30 |
And we ran the following SQL statement which creates a cross-tabulation query using the PIVOT clause:
It would return the following result:
TotalSalaryByDept | 30 | 45 |
---|---|---|
TotalSalary | 122500 | 176000 |
This example would create a pivot table to display the total salary for dept_id 30 and dept_id 45. The results are displayed in one row with the two departments appearing each in their own column.
Now, let's break apart the PIVOT clause and explain how it worked.
First, we want to specify what fields to include in our cross tabulation results. In this example, we want to include the literal value 'TotalSalary' as the first column in the pivot table. And we want to create one column for dept_id 30 and a second column for dept_id 45. This gives us 3 columns in our pivot table.
Next, we need to specify a SELECT statement that will return the source data for the pivot table.
In this example, we want to return the dept_id and salary values from the employees table:
You must specify an alias for the source query. In this example, we have aliased the query as SourceTable.
Next, we need to specify what aggregate function to use when creating our cross-tabulation query. You can use any aggregate such as SUM, COUNT, MIN, MAX, or AVG functions.
In this example, we are going to use the SUM function. This will sum the salary values:
Finally, we need to specify what pivot values to include in our results. These will be used as the column headings in our cross-tabulation query.
In this example, we are going to return only the dept_id values of 30 and 45. These values will become our column headings in our pivot table. Also, note that these values are a finite list of the dept_id values and will not necessarily contain all possible values.
Now when we put it all together, we get the following pivot table:
TotalSalaryByDept | 30 | 45 |
---|---|---|
TotalSalary | 122500 | 176000 |