Hello all. I am trying to get a grip on the pivot query syntax.
I have about 20 fields worth of aggregate data all associated with 1 field, which is the category for those 20 fields (by fields I mean columns).
It looks like this
Category1 column1 column2 column3
Category2 column1 column2 column3
and so on...
What I am trying to do is pivot those results to come out like this
Category1 Category2 Category3
column1 column1 column1
column2 column2 column2
column3 column3 column3
My query looks like this
SELECT TOP(3) category FROM
(
SELECT category FROM table
)p
PIVOT
(
AVG(column1) as column1, AVG(column2) as column2, AVG(column3) as column3)
FOR category IN category
) AS pvt;
I am running SQL Server 2005. Thanks for any help or suggestions!