Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

i have requirement like to Convert rows to columns in SQL server.

i have retrieved data like below:
SQL
MatchId SelectionName   Odds
322	Home Win	1.33
322	Draw	        5.00
322	Away Win	9.00
322	Under 2.5	2.40
322	Over 2.5	1.53

and want to convert like below:
SQL
MatchId Home Win   Draw   Away Win   Under 2.5   Over 2.5
322	1.33	   5.00   9.00       2.40        1.53 

how can i do this?

Thanks,
KK
Posted
Updated 27-May-13 1:33am
v2

SQL
Select MatchId,[Home Win],[Draw],[Away Win],[Under 2.5],[Over 2.5]
From TableName 
PIVOT
( 
    SUM(Odds) For SelectionName in ([Home Win],[Draw],[Away Win],[Under 2.5],[Over 2.5])
) as pvttbl

Happy Coding!
:)
 
Share this answer
 
Comments
Arun Vasu 27-May-13 7:49am    
+5!
Good..
Aarti Meswania 27-May-13 7:53am    
Thank you! :)
query for make columns into rows in ms sql server[^]



use this.. If you have any query Please let me know.. i will help you..
 
Share this answer
 
Hello,

i got my result from below query.

SQL
select * from (select MatchId,SelectionName,Odds From
    #tmpMatchOdds) As P Pivot
    (
        Sum([odds]) For SelectionName In ([Home Win],[Draw],[Away Win],[Under 2.5],[Over 2.5])
    )As pv



Thanks for reply.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900