Click here to Skip to main content
11,484,402 members (64,506 online)
Rate this: bad
good
Please Sign up or sign in to vote.
Hi All,

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

i have retrieved data like below:
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:
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 27-May-13 2:30am
kk20141.9K
Edited 27-May-13 2:33am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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!
Smile | :)
  Permalink  
Comments
Arun Vasu at 27-May-13 7:49am
   
+5!
Good..
Aarti Meswania at 27-May-13 7:53am
   
Thank you! :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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..
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hello,

i got my result from below query.

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.
  Permalink  

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

  Print Answers RSS
0 Maciej Los 190
1 Sergey Alexandrovich Kryukov 170
2 Raje_ 155
3 OriginalGriff 147
4 Mehdi Gholam 120
0 Sergey Alexandrovich Kryukov 8,484
1 OriginalGriff 7,948
2 Sascha Lefèvre 3,189
3 Maciej Los 2,601
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 27 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100