首页 > 代码库 > SQL多行合并成一行

SQL多行合并成一行

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

SQL多行合并成一行