Please try this. Hope this will help, here your table1 is TestConC
select a.id, a.val1, CustomData
= STUFF( (SELECT ',' + b.val3
FROM TestConC b
where b.id=a.id and b.val1=a.val1
FOR XML PATH('')),1, 1, '')
From TestConC a
group by a.id, a.val1
order by a.id