临时记录--sql 合并同一列中的值

select PR.Id, PR.Name as RoleName, Left(Final.UserList, LEN(Final.UserList) - 1) as Members
from ProjectRoles PR
left join (select ProjectRoleId,(
select Name + ','
from (select M.ProjectRoleId, U.Name
from ProjectMembers M
join Users U on M.UserId = U.Id) SourceTB
where SourceTB.ProjectRoleId = PM.ProjectRoleId
order by ProjectRoleId for xml path('')) as UserList
from (select M.ProjectRoleId, U.Name
from ProjectMembers M
join Users U on M.UserId = U.Id) PM
group by ProjectRoleId) Final on PR.Id = Final.ProjectRoleId

有点乱 !!

posted @ 2012-01-03 16:09  西天之旅  阅读(216)  评论(0编辑  收藏  举报