Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Pivots with Dynamic Columns in SQL Server 2005/2008 - MS SQL Dev

Sign in | Help

in MS SQL Dev Search

Eric Vaillancourt on SQL Server...

● Home ● Blogs ● Forums ● Downloads

MS SQL Dev This Blog


● Home

Pivots with Dynamic Columns in SQL Server 2005/2008 ● Contact

Syndication
Pivots in SQL Server 2005/2008 can convert row into column data. Pivots are frequently used in reports, and are
reasonably easy to work with. ● RSS
● Atom
However, many people have asked me how to make the column list dynamic. Normally, this list is fixed, but ● Comments RSS
many times the new columns are determined by the data at a later stage. This problem is easily solved when we

mix pivots with dynamic SQL, so here is a very simple example about how to dynamically generate the pivot
Receive Email Updates
statement:
Your Email Address Subscribe
PIVOT allows you to turn data rows into columns. For example, if you have a query like this:

USE AdventureWorks
GO Recent Posts
SELECT * FROM
(SELECT CustomerID, DATEPART(m, OrderDate) OrderMonth, SubTotal ● Pivots with Dynamic Columns in SQL Server 2005/2008
FROM Sales.SalesOrderHeader ● Functions to Work with NULL Values
WHERE OrderDate between '20030101' and '20031231' ● How to Use Aggregate Functions with NULL Values
and CustomerID IN (2,4,6,7,8,9)) src
● How to combine multiple rows of data on same line.
PIVOT (SUM(SubTotal) FOR OrderMonth
IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) AS pvt
GO
Tags

AggregateCoalesceCount(*)DYNAMICFOR XML

PATH IsNullnullNullIfPIVOTReportSelectSTUFFTrick
http://www.sqlprof.com/blogs/sqldev/archive/2008/04/12/pivots-with-dynamic-columns-in-sql-server-2005-2008.aspx (1 of 7)8/29/2010 9:20:00 PM
Pivots with Dynamic Columns in SQL Server 2005/2008 - MS SQL Dev

SQL Blog that I read


● Greg Linwood
● Rob Farley
● Bob Beauchemin
● Kimberly Tripp

Archives
● April 2008 (3)
● March 2008 (1)

The rows coming from the internal query are transposed into colums:

SELECT * FROM
(SELECT CustomerID, DATEPART(m, OrderDate) OrderMonth, SubTotal
FROM Sales.SalesOrderHeader
WHERE OrderDate between '20030101' and '20031231'
and CustomerID IN (2,4,6,7,8,9)) src
PIVOT (SUM(SubTotal) FOR OrderMonth
IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) AS pvt

This is ok if the colum list suppied in the IN clause is fixed like the 12 months of the year. But if the list evolves with the data, you would have to re-write your pivot. Not to
practical.

Let’s look at another example:

SELECT * FROM
(SELECT CustomerID, YEAR(OrderDate) OrderYear, SubTotal
FROM Sales.SalesOrderHeader
WHERE CustomerID >=1 and CustomerID <=35) src
PIVOT (SUM(SubTotal) FOR OrderYear
IN ([2002],[2003])) AS pvt
GO

In this pivot example, we want to display a colums for each year and a row for each customer:

http://www.sqlprof.com/blogs/sqldev/archive/2008/04/12/pivots-with-dynamic-columns-in-sql-server-2005-2008.aspx (2 of 7)8/29/2010 9:20:00 PM


Pivots with Dynamic Columns in SQL Server 2005/2008 - MS SQL Dev

Notice that we are only displaying data for years 2002 and 2003. We could modify our pivot to make it dynamic.

Look at the following code:

DECLARE @listCol VARCHAR(2000)


SELECT @listCol = STUFF(( SELECT DISTINCT
'],[' + ltrim(str(YEAR(OrderDate)))
FROM Sales.SalesOrderHeader
ORDER BY '],[' + ltrim(str(YEAR(OrderDate)))
FOR XML PATH('')
), 1, 2, '') + ']'

By using the combination of STUFF and FOR XML PATH we can build a string that concatenates all columns by doing a SELECT DISTINCT on our table before doinf the
pivot.

After executing this piece of code, you should get in the @listCol variable the list of colums needed to do the pivot.

http://www.sqlprof.com/blogs/sqldev/archive/2008/04/12/pivots-with-dynamic-columns-in-sql-server-2005-2008.aspx (3 of 7)8/29/2010 9:20:00 PM


Pivots with Dynamic Columns in SQL Server 2005/2008 - MS SQL Dev

Now, all we have to do is to convert our pivot into a dynamic query:

DECLARE @listCol VARCHAR(2000)


DECLARE @query VARCHAR(4000)
SELECT @listCol = STUFF(( SELECT DISTINCT
'],[' + ltrim(str(YEAR(OrderDate)))
FROM Sales.SalesOrderHeader
ORDER BY '],[' + ltrim(str(YEAR(OrderDate)))
FOR XML PATH('')
), 1, 2, '') + ']'

SET @query =
'SELECT * FROM
(SELECT CustomerID, YEAR(OrderDate) OrderYear, SubTotal
FROM Sales.SalesOrderHeader
WHERE CustomerID >=1 and CustomerID <=35) src
PIVOT (SUM(SubTotal) FOR OrderYear
IN ('+@listCol+')) AS pvt'

EXECUTE (@query)

Et voila:

http://www.sqlprof.com/blogs/sqldev/archive/2008/04/12/pivots-with-dynamic-columns-in-sql-server-2005-2008.aspx (4 of 7)8/29/2010 9:20:00 PM


Pivots with Dynamic Columns in SQL Server 2005/2008 - MS SQL Dev

Hope this was helpful,

Eric Vaillancourt
www.sqlprof.com

http://www.sqlprof.com/blogs/sqldev/archive/2008/04/12/pivots-with-dynamic-columns-in-sql-server-2005-2008.aspx (5 of 7)8/29/2010 9:20:00 PM


Pivots with Dynamic Columns in SQL Server 2005/2008 - MS SQL Dev

Technorati : SQL Server 2005,Dynamic Select,PIVOT

Published Apr 12 2008, 10:13 AM by Eric Vaillancourt

Filed under: Trick, FOR XML PATH, DYNAMIC, STUFF, PIVOT

Comments
DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

April 12, 2008 10:43 AM


thorkyl said:

This is fantastic, well written.

April 30, 2008 4:42 PM


How to pivot data | keyongtech said:

Pingback from How to pivot data | keyongtech

January 18, 2009 11:41 AM


?????????????????? » SQL?????????????????? 9?????? said:

Pingback from ?????????????????? &raquo; SQL?????????????????? 9??????

May 29, 2010 2:49 PM


?????????????????? » SQL?????????????????? 10?????? said:

Pingback from ?????????????????? &raquo; SQL?????????????????? 10??????

June 5, 2010 7:38 AM

About Eric Vaillancourt


Eric Vaillancourt possède plus de vingt ans d'expérience en programmation de base de données et en gestion de projets. Depuis une dizaine d’années, il se spécialise en optimisation
des performances et en coaching d’administrateurs de bases de données. Il a occupé des postes de haute direction dans le secteur privé, principalement dans des firmes
technologiques. Il a eu l'occasion de gérer plusieurs projets liés au développement des affaires et aux changements organisationnels.

http://www.sqlprof.com/blogs/sqldev/archive/2008/04/12/pivots-with-dynamic-columns-in-sql-server-2005-2008.aspx (6 of 7)8/29/2010 9:20:00 PM


Pivots with Dynamic Columns in SQL Server 2005/2008 - MS SQL Dev

©2008 SQLProf.com & Eric Vaillancourt

http://www.sqlprof.com/blogs/sqldev/archive/2008/04/12/pivots-with-dynamic-columns-in-sql-server-2005-2008.aspx (7 of 7)8/29/2010 9:20:00 PM

You might also like