select a.* from (select id,Detail,partId=(select cast(partId as char(36))+',' from (select * from table) as b where b.id=a.id for xml path(''))
,code
from table a
group by id,Detail,code) a