1

Hi my original data is as below

Id   | Value
-----+------
0001 | IN
0001 | ME
0001 | OH
0001 | ON
0002 | AC
0002 | ON
0002 | VI
0002 | ZO
0003 | ME
0003 | OO
0003 | PS
0003 | QA

Wanted to get the Data in

Id   | Value
-----+------
0001 | IN,ME,OH,ON
0002 | AC,ON,VI,ZO
0003 | ME,OO,PS,QA

in SQL server 2008

Cœur
  • 37,241
  • 25
  • 195
  • 267
rocky_pps
  • 143
  • 2
  • 2
  • 8
  • I have tested the query in sql server ,,, it's work well you can utilize the answer, and dont forget the answer to mark as correct so it can help other in future. – Dilip Suvagiya Feb 06 '14 at 13:56

2 Answers2

0

This will work :

  SELECT
  t1.ID,
  MemberList = substring((SELECT ( ', ' + Value )
                       FROM your_table t2
                       WHERE t1.ID = t2.ID
                       ORDER BY 
                          ID,
                          Value
                       FOR XML PATH( '' )
                      ), 3, 1000 )FROM your_table t1
   GROUP BY ID
Dilip Suvagiya
  • 396
  • 1
  • 4
  • 14
0
    drop table #t
    
create table #t(id varchar(10),value char(10))

insert into #t values('0001','IN'),
('0001','ME'),
('0001','OH'),
('0001','ON'),
('0002','AC'),
('0002','ON'),
('0002','VI'),
('0002','ZO'),
('0003','ME'),
('0003','OO'),
('0003','PS'),
('0003','QA')

select distinct id,
    stuff((select ',' + CAST(t2.Value as varchar(10))
     from #t t2 where t1.id = t2.id 
     for xml path('')),1,1,'') as Value
from #t t1
group by id,Value

Output:

enter image description here

See Demo

Community
  • 1
  • 1
vhadalgi
  • 7,027
  • 6
  • 38
  • 67