• subscribe
June 23, 2009 12:00 AM

Create Pivoted Tables in 3 Steps

PIVOT query uses a CTE
SQL Server Pro
InstantDoc ID #101684
Downloads
101684.zip

Step 3. Add the Column Names to the SELECT List

At this point, you need to add the column names to the SELECT list in the main query. In this case, the columns are OrderYear and the pivoted columns. You should not list the aggregated column, TotalDue, so the SELECT list looks like

SELECT OrderYear,January,February,
  March,April,May,June,July,August,
  September,October,November,December

The pivoted columns will display in the order that they're listed in the SELECT clause. Listing 4 shows the completed PIVOT query, which created the pivoted results in Table 2.

Listing 4: The PIVOT Expression


Variations

There are many ways the PIVOT query can vary. Take, for example, the PIVOT query in Listing 5.

Listing 5: Another Example of a PIVOT Query

This query varies from the one in Listing 4 two ways:

  • It uses aliased column names. In this query, the numeric month of the order date is used in the base query. The pivoted column names are aliased in the SELECT list so that the column headers are month names rather than month numbers. To save typing, it's advantageous to produce the desired column headings in the base query, which in this case is the CTE.
  • It uses an ORDER BY clause to order the returned data. The ORDER BY clause needs to go after the PIVOT expression. The only columns allowed in the ORDER BY clause are those that actually show up as columns in the results. You can include the columns used for grouping and those specified in the IN list. For example, this query uses ORDER BY January DESC to display the results in order of highest to lowest sales in January. If you want to display the year in descending order, you would put ORDER BY OrderYear DESC after the PIVOT expression.
A Useful Tool

Although the PIVOT operator might look intimidating, writing a PIVOT query isn't that difficult if you take it step by step. The PIVOT operator is perfect for pivoting results when the pivoted columns aren't likely to change. Although it's disappointing that the PIVOT operator isn't dynamic, the PIVOT operator is still a very useful tool. (A dynamic PIVOT operator is on my wish list for the next release of SQL Server since the feature didn’t make it into SQL Server 2008.)



ARTICLE TOOLS

Comments
  • Greg
    3 years ago
    Jul 13, 2009

    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

  • Megan
    3 years ago
    Jul 06, 2009

    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

  • Pei
    3 years ago
    Jul 06, 2009

    link for code download did not work.

You must log on before posting a comment.

Are you a new visitor? Register Here