0

I have a table with 2 columns ID (not PK) and NAME. I want to take all the names that have the same ID and connect them into one column - how can I do this?

e.g.

ID                 Name
----               ----
1                   A
2                   B     
3                   C   
3                   D          
5                   E
3                   F

result :

1   A
2   B
3   CDF
5   E
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    In MySQL it's `GROUP_CONCAT()` - see these answer for Oracle versions: http://stackoverflow.com/questions/1120706/is-there-an-oracle-sql-query-that-aggregates-multiple-rows-into-one-row http://stackoverflow.com/questions/16771086/is-there-any-function-in-oracle-similar-like-group-concat-of-mysql – Daniel W. Oct 08 '13 at 07:34
  • Did any of the answers work? – Mad Dog Tannen Oct 08 '13 at 20:41

2 Answers2

1

Try this code

SELECT Id,GROUP_CONCAT(Name SEPARATOR ' ') FROM tablename GROUP BY Id;

In Oracle the GROUP_CONCAT() is name wm_concat().

Mad Dog Tannen
  • 7,129
  • 5
  • 31
  • 55
0

With MS Sql Server you may want to try this:

create table test
( id int, name varchar(10))

insert test (id,name) values (1,'A')
insert test (id,name) values (2,'B')
insert test (id,name) values (3,'C')
insert test (id,name) values (3,'D')
insert test (id,name) values (5,'E')
insert test (id,name) values (4,'F')


SELECT DISTINCT id AS Tabelle,
                Spalten = STUFF((SELECT DISTINCT ',' + name
                                   FROM test a
                                  WHERE a.id = b.id
                                 FOR XML PATH ('')), 1, 1, '')
from test b
Reto
  • 102
  • 3