Crosstab queries using PIVOT in SQL Server 2005

Here is pivot table query i made

USE [ProjectServer_Reporting]
GO
/****** Object:  View [dbo].[CADTH_FTE_PivotOnProgram]    Script Date: 05/10/2011 08:58:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[CADTH_FTE_PivotOnProgram]
AS
SELECT ResourceUID AS ResourceUID,
[Inquiry], [RR1], [RR15_2_3], [Form], [ES], [TS], [OU]
FROM
(SELECT ResourceUID, ResourceName, ReportCode, FTEAllocated
FROM .Cadth_FTE_ResourceLevel) AS SourceTable
PIVOT
(
Sum(FTEAllocated)
FOR ReportCode IN ([Inquiry], [RR1], [RR15_2_3], [Form], [ES], [TS], [OU]
)
) AS PivotTable;

 

 

 

 

By: Greg Robidoux | Comments (1) | Print |

Share5

Expand your SQL Server horizons with a Kindle loaded with 5 eBooks

    Read these related tips:

Problem
In SQL Server 2000 there was not a simple way to create cross-tab queries, but a new option in SQL Server 2005 has made this a bit easier. We took a look at how to create cross-tab queries in SQL Server 2000 in this previous tip and in this tip we will look at this new feature in SQL Server 2005 to allow you produce cross-tab results.

Solution
With SQL Server 2005 a lot of new features have been introduced. One of these new features is PIVOT.  What this allows you to do is to turn query results on their side, so instead of having results listed down like the listing below, you have results listed across.

SalesPerson
Product
SalesAmount

Bob
Pickles
$100.00

Sue
Oranges
$50.00

Bob
Pickles
$25.00

Bob
Oranges
$300.00

Sue
Oranges
$500.00

With a straight query the query results would be listed down, but the ideal solution would be to list the Products across the top for each SalesPerson, such as the following:

SalesPerson
Oranges
Pickles

Bob
$300.00
$125.00

Sue
$550.00

To use PIVOT you need to understand the data and how you want the data displayed.  First you have the data rows, such as SalesPerson and the columns, such as the Products and then the values to display for each cross section.  Here is a simple query that allows us to pull the cross-tab results.

SELECT SalesPerson, [Oranges] AS Oranges, [Pickles] AS Pickles
FROM
(SELECT SalesPerson, Product, SalesAmount
FROM ProductSales ) ps
PIVOT
(
SUM (SalesAmount)
FOR Product IN
( [Oranges], [Pickles])
) AS pvt

So how does this work?

There are three pieces that need to be understood in order to construct the query.

  • (1) The SELECT statement
    • SELECT SalesPerson, [Oranges] AS Oranges, [Pickles] AS Pickles
    • This portion of the query selects the three columns for the final result set (SalesPerson, Oranges, Pickles)
  • (2) The query that pulls the raw data to be prepared
    • (SELECT SalesPerson, Product, SalesAmount FROM ProductSales) ps
    • This query pulls all the rows of data that we need to create the cross-tab results.  The (ps) after the query is creating a temporary table of the results that can then be used to satisfy the query for step 1.
  • (3) The PIVOT expression
    • PIVOT (SUM (SalesAmount) FOR Product IN ( [Oranges], [Pickles]) ) AS pvt
    • This query does the actual summarization and puts the results into a temporary table called pvt

Another key thing to notice in here is the use of the square brackets [ ] around the column names in both the SELECT in part (1) and the IN in part (3).  These are key, because the pivot operation is treating the values in these columns as column names and this is how the breaking and grouping is done to display the data.

Next Steps

  • Take a look at the new PIVOT option that SQL Server 2005 offers to see how you can use this
  • Review the older methods that were discussed in this prior tip.  These options will still work in SQL Server 2005 and may provide a better solution.
  • Try to write some more complex queries to take advantage of this new option.
Advertisements