|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
question-How to combine rows
Hello,
I was wondering if anyone knows a SQL command that will combine several rows into one row. For example if I have the following data: column1 column2 1 a 1 b 1 c 1 d 1 e I would like the select statement to return the following: column1 column2 1 a;b;c;d;e; Any help would be greatly appreciated. thanks |
|
#2
|
|||
|
|||
|
hi,
i had to solve a similar problem where the scores of a chess league are managed in a table like this one. date place team score --------------------- d1 p1 t1 5 d1 p1 t5 3 d1 p2 t3 2.5 d1 p2 t8 5.5 d2 p5 t1 4 d2 p5 t3 4 d3 p2 t8 2 d3 p2 t2 6 All pairs of rows with the same date/place entry had to be combined into one row and virtually stored in a view for furhter processing, for example: d1, p2 gives row d1 p2 t3 2.5 t8 5.5. This task can be done by the following self-join. create view scorestable as select a.date, a.place, a.team as 'tm1', a.score as 'se1', b.team as 'tm2', b.score as 'se2' from gameresults a join gameresults b on a.date = b.date and a.place = b.place and a.team < b.team select * from scorestable date place tm1 se1 tm2 se2 d1 p1 t1 5.0 t5 3.0 d1 p2 t3 2.5 t8 5.5 d2 p5 t1 4.0 t3 4.0 d3 p2 t2 6.0 t8 2.0 (Most important is join condition "and a.team < b.team" to avoid redundancy. This has been carried out on sybase sqlanywhere.) I hope my solution for combining two rows into one helps you. tesuji |
![]() |
| Viewing: Dev Articles Community Forums > Databases > Microsoft SQL Server > question-How to combine rows |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|