Databases Reference
In-Depth Information
Figure 4-5.
Using the
PIVOT
operator to summarize data
How It Works
You begin with the
SELECT
list and specify the columns and their aliases as you want them
to appear in the result set.
select CardType ,[2006] as Year2006,[2007] as Year2007
from
Then you specify the
SELECT
statement for the table with column names from which
you will be retrieving data, and you also assign a
PIVOT
operator to the
SELECT
statement.
select CardType,ExpYear
from Sales.CreditCard
) piv Pivot
Now you need to count the cards of particular type for the years 2006 and 2007 as
specified in this statement:
(
count(ExpYear) for ExpYear in ([2006],[2007])
)as carddetail
Search WWH ::
Custom Search