col1 col2 col2
m1 1 a
m1 1 b
m1 1 c
m2 2 d
m2 2 e
m2 1 f
output
---------
m1 1,a,b,c,f
m2 2,d,e
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Guest UserRank #259 · 9.8K reputationPosted Jul 21, 2014, 1:12 AM EDT
You can google for examples on the same
Khan Abrar AhmedRank #778 · 3K reputationPosted Aug 6, 2014, 9:31 PM EDT
declare @table as table (col1 varchar(64),col2 int,col3 varchar(64))
insert into @table values ('m1',1,'a'),('m1',1,'b'),('m1',1,'c')
,('m2',2,'d'),('m2',2,'e'),('m1',1,'f')
select distinct col1,col2,(
select col3+',' from @table t1 where t1.col2=t2.col2 and t1.col1=t2.col1 for xml path(''))
from @table t2