原文地址:https://blog.csdn.net/u010673842/article/details/79637618
select ID,
STUFF((select ',' + name from class where ID = a.ID for xml path('')),1,1,'')
from class a group by ID
--测试 SQL GROUP 组字符串拼接
SELECT
NCOMPONENTCODE,
STUFF((select ',' + NCOMPONENTCODE from TB_PROJECTS10 where NCOMPONENTCODE = a.NCOMPONENTCODE for xml path('')),1,1,'') AS NCOMPONENTCODES,
STUFF((select ',' + PROJECTNAME from TB_PROJECTS10 where NCOMPONENTCODE = a.NCOMPONENTCODE for xml path('')),1,1,'') AS PROJECTNAMES
from TB_PROJECTS10 a group by NCOMPONENTCODE
--查看重复的构件编码
SELECT * FROM (
SELECT
NCOMPONENTCODE,
COUNT(1) AS RepeatNum,
--分部分项名称
STUFF(
(
SELECT
',' + PROJECTNAME
FROM TB_PROJECTS10
WHERE NCOMPONENTCODE = a.NCOMPONENTCODE
FOR XML PATH('')
)
,1,1,'') AS 'PROJECTNAME 分部分项名称 多个以逗号隔开'
FROM dbo.TB_PROJECTS10 a
GROUP BY NCOMPONENTCODE
) T ORDER BY RepeatNum DESC