Professional Documents
Culture Documents
Pivots With Dynamic Columns in SQL Server 2005 2008
Pivots With Dynamic Columns in SQL Server 2005 2008
Sign in | Help
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
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.
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:
Notice that we are only displaying data for years 2002 and 2003. We could modify our pivot to make it dynamic.
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.
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:
Eric Vaillancourt
www.sqlprof.com
Comments
DotNetKicks.com said: