Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
morikoth
Starting Member
5 Posts |
Posted - 2012-01-16 : 08:15:49
|
| Hello,I have a table like that:ID phonenumber contact1 29349245934 a2 34953405823 a3 34538458345 a4 93459345394 b5 54688456854 b6 56786587856 c7 54684568456 c8 34858129349 cI want to write a script which return me that tableContact PhoneNB1 PhoneNB2 PhoneNB3a 29349245934 34953405823 34538458345 b 93459345394 54688456854 c 54684568456 34858129349 How can i do that?i tried with while and pivot but it takes a long time to run. Any other ideas? Thx,Marius |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-16 : 10:04:00
|
| [code]SELECT contact,MAX(CASE WHEN Seq=1 THEN phonenumber END) AS PhoneNB1,MAX(CASE WHEN Seq=2 THEN phonenumber END) AS PhoneNB2,MAX(CASE WHEN Seq=3 THEN phonenumber END) AS PhoneNB3FROM(SELECT *,ROW_NUMBER() OVER (PARTITION BY contact ORDER BY ID) AS SeqFROM Table)tGROUP BY contact[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|