June 23, 2009 08:23 PM

Create Pivoted Tables in 3 Steps

PIVOT query uses a CTE
Rating: (0)
SQL Server Magazine
InstantDoc ID #101684
Executive Summary:
Many people find the PIVOT operator syntax in SQL Server Books Online (BOL) hard to understand. The syntax in SQL Server Books Online uses a derived table as the basis for the PIVOT query. What SQL Server Books Online doesn't point out is that you can use a common table expression (CTE) instead. Kathi Kellenberger walks you through creating a PIVOT query that uses a common table expression.

...

...This article is for paid Professional Members only.

Already a Professional Member? Please log in now:

NOT A PROFESSIONAL MEMBER? YOU CHOOSE:

Professional Membership

Monthly

Annual

VIP Membership

Monthly

Annual

Add a Comment

Kathi,

Thank you for the excellent article on how to build a PIVOT query in 3 steps. Your step by step approach made it very easy to understand how to write a PIVOT query. I expect after reading your article people will be a lot less stressed and will have a good handle on resolving issues when they trying working through developing their PIVOT queries based on our article.

I suppose it was a slight oversight on Microsoft's behalf that BOL doesn't state specifically that a CTE could be used as a data source for the PIVOT query. But to be fair to Microsoft they did state you can use a "derived table" as a table source for a PIVOT query. Since a CTE is a temporary named result set, one could argue (not that I am) this is a "derived table".


One last note - It seems that callout "B" in listing 2 is missing the word "PIVOT" prior the first left parentheses ("("). But then again maybe I've missed the point of this listing.
Thanks for the excellent article,
Greg

Greg7/13/2009 12:52:51 PM


Hi pzhu1968,

Thanks so much for letting us know that the Download the Code link wasn't working. The Download the Code link has been fixed.

Thanks again!

Megan Keller
Associate Editor, SQL Server Magazine
mkeller@sqlmag.com

Megan7/6/2009 4:47:40 PM


link for code download did not work.

Pei7/6/2009 1:18:21 PM


You must log on before posting a comment.

Are you a new visitor? Register Here
GOOGLE LINKS
SPONSORED LINKS
FEATURED LINKS