SQL多行合并成一行

时间:2022-08-13 10:26:37

with cte as(
select groupname,GroupId from Sys_Group where GroupId in(
select ReleaseRangeId from Sys_Notice_Permission where ReleaseRangeType=2 and NoticeId=20)
)

select LEFT(groupname,LEN(groupname)-1) as groupname,
LEFT(GroupId,LEN(GroupId)-1) as GroupId

from
(
SELECT (SELECT groupname+',' from cte FOR XML PATH('') ) as groupname,
(SELECT cast(GroupId as nvarchar)+',' from cte FOR XML PATH('') ) as GroupId

) A