TableA
ID Names
1 n1
2 n2
1 nn2
2 nn3
3 nn4
1 nn1
ID相同的记录,对Names进行聚合
得到结果是:
ID SumNames
1 n1,nn2,nn1
2 n2,nn3
3 nn4
mysql
select id,group_concat(names) from tableA group by id;
mssql
select stuff((select ','+names from tableA as t2 where t2.id=t1.id for xml path('')),1,1) from (select id,names from tableA as t1 group by id);
select stuff(
(select ','+convert(Varchar(50),MemberGuid) from (SELECT a.MemberGuid,a.managerguid,b.UserName FROM membermanager a LEFT JOIN k_manager b ON a.managerguid = b.managerguid) as t2 where t2.MemberGuid = t1.MemberGuid FOR XML PATH('')), 1, 1, ''
) from (SELECT a.MemberGuid,a.managerguid,b.UserName FROM membermanager a LEFT JOIN k_manager b ON a.managerguid = b.managerguid) as t1
where memberguid=@MemberGuid group by memberguid