View Full Version : PIVOT question. Please HELP!


mapat
04-27-2011, 06:21 AM
Hello,

I created a temp "#ToPivot" table that holds the following values:

ServerName Date CountHits
ServerA 4/25/2011 4
ServerA 4/26/2011 2

How do I generate a table based on the above one that would look like this:

ServerName 4/25/2011 4/26/2011
ServerA 4 2

This is the code I tried:
SELECT [ServerName], 04/25/11, 04/26/11
FROM
(SELECT ServerName,
[Date],
Hits
FROM #ToPivot
) AS Src
PIVOT
(
SUM(Src.Hits)
FOR Src.[Date] IN ([4/25/2011], [4/26/2011])
) AS pvt

and it gives me the following result set:
ServerName (No column name) (No column name)
HNWAS3535 0 0
NPWAS5065 0 0

I would really appreciate a hand on this since I need to generate this data as soon as possible.

Thank you very much everyone :)

lcook1974
05-26-2011, 04:43 AM
and it gives me the following result set:
ServerName (No column name) (No column name)
HNWAS3535 0 0
NPWAS5065 0 0



You didn't give them column names in the statement

SELECT [ServerName], 04/25/11 AS [columnA], 04/26/11 as [columnB]
FROM
(SELECT ServerName,
[Date],
Hits
FROM #ToPivot
)AS Src
PIVOT
(
Count(Src.Hits)
FOR Src.[Date] IN([4/25/2011], [4/26/2011])
)AS pvt





Try that...

if you got it figured out let us know.