Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 5th, 2005, 09:03 PM
alchemist alchemist is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 1 alchemist User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 24 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old February 26th, 2005, 10:07 PM
tesuji tesuji is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 3 tesuji User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 26 m 25 sec
Reputation Power: 0
Cool self-join may help you

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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMicrosoft SQL Server > question-How to combine rows


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT