WiseOwl Training - Established 1992 Wise Owl Training

Established May 1992
30 years in business
Wise Owl Training
30 years in business
See 519 reviews for our classroom and online training
Pivoting Data in SQL Server
Part one of a three-part series of blogs

Using the Pivot operator in SQL Server allows you to create basic pivot tables from your query results. This blog teaches you how to create simple, static pivots, crosstab-style tables and exciting, dynamic pivot tables.

  1. Pivoting Data in SQL Server (this blog)
  2. Using the Pivot Operator in SQL Server
  3. Dynamic Pivot Tables

Posted by Andrew Gould on 09 June 2014

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Pivoting Data in SQL Server

You can download the script to create the database used in this article here.

Our two-day advanced SQL course usually has time to cover this topic.  Failing this, there are loads more SQL training resources here.

Grouping and Aggregating Data

The standard way to group and aggregate data in SQL Server is to use the GROUP BY clause.



,COUNT(FilmID) AS [Count of Films]



tblCountry AS c

ON c.CountryID=f.FilmCountryID



Executing the query shown above generates the following results:

Results of group by

The GROUP BY clause generates results grouped in rows.


The row groups generated by this type of query work well enough, but what if you wanted the data grouped by columns instead? What if you wanted both row groups and column groups in the same query?

Column groups

You might want to organise you groups in columns rather than rows.

Row and column groups

You may want to create both row and column groups to create a crosstab-style report.

You can achieve results such as the ones shown above using the PIVOT operator and the rest of this article explains how it works.

  1. Pivoting Data in SQL Server (this blog)
  2. Using the Pivot Operator in SQL Server
  3. Dynamic Pivot Tables
This blog has 0 threads Add post