PIVOT question. Please HELP!

mapat

Registered User.
Local time
Today, 01:42
Joined
Feb 2, 2007
Messages
176
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 :)


 
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
Code:
[I][COLOR=#0000ff]SELECT[/COLOR][SIZE=2] [ServerName][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080],[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] 04[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]/[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]25[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]/[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]11 [/SIZE][SIZE=3][COLOR=red]AS [columnA][/COLOR][/SIZE][SIZE=2][SIZE=2][COLOR=red],[/COLOR][/SIZE][/SIZE][SIZE=2] 04[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]/[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]26[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]/[/COLOR][/SIZE][/COLOR][/SIZE][/I][I][SIZE=2]11 [/SIZE][SIZE=3][COLOR=red]as [columnB]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]FROM[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]
[/SIZE][I][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]([/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]SELECT[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ServerName[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080],
[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2][I][Date][/I][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080][I],[/I]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][I]Hits[/I]
[/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][I]FROM[/I][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][I] #ToPivot [/I]
[/SIZE][I][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080])[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]AS[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Src
[/SIZE][/I][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080][I]PIVOT[/I]
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080][I]([/I]
[/COLOR][/SIZE][/COLOR][/SIZE][I][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080][SIZE=3][COLOR=red][B]Count[/B][/COLOR][/SIZE]([/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]Src[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080].[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]Hits[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080])
[/COLOR][/SIZE][/COLOR][/SIZE][/I][I][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]FOR[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] Src[/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080].[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][Date] [/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]IN[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080]([/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][4/25/2011][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080],[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] [4/26/2011][/SIZE][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080])
[/COLOR][/SIZE][/COLOR][/SIZE][/I][I][SIZE=2][COLOR=#808080][SIZE=2][COLOR=#808080])[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]AS[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] pvt
[/SIZE][/I][SIZE=2]
[/SIZE]


Try that...

if you got it figured out let us know.
 

Users who are viewing this thread

Back
Top Bottom