/* 分组之后拼接字符串 */ ;with t as( SELECT WorkflowId,Remark FROM dbo.OperatorAutomationProcess GROUP BY WorkflowId,Remark ) , H AS( SELECT WorkflowId, -- 分组的主键 STUFF( ( SELECT '_'+ Remark -- 要拼接的列 FROM t a WHERE b.WorkflowId = a.WorkflowId FOR XML PATH('') ),1 ,1, '') Remark FROM t b GROUP BY WorkflowId ) SELECT * FROM H
要分组的数据:
按照分组 拼接后的数据: