Q&A about the site for physical fitness professionals, athletes, trainers, and those providing health-related needs
DECLARE @tmp TABLE( Id int, TagNo int, TermNo int) INSERT INTO @tmp (Id, TagNo, TermNo) VALUES (1,1000,2), (2,1000,3), (3,1000,7), (4,1002,1), (5,1002,10) SELECT t1.TagNo, List = SUBSTRING(( SELECT ( ', ' + CAST(TermNo AS varchar(max)) ) FROM @tmp t2 WHERE t1.TagNo = t2.TagNO ORDER BY TagNo, TermNo FOR XML PATH( '' )), 3, 1000 ) FROM @tmp t1 GROUP BY t1.TagNo