0
ID  | NAME  
----|--------  
1   |Ann  
2   |Jake  
1   |Julie  
3   |Paul  
2   |Shane  
4   |Kumi

我想使用stuff()如下连接值。并且单个值不应受stuff()功能影响。

ID  | NAME  
----|--------  
1   |Ann,Julie  
2   |Jake,Shane  
3   |Paul  
4   |Kumi

怎么做?

4

2 回答 2

2
CREATE TABLE #A
(ID INT, NAME VARCHAR(10))
INSERT INTO #A VALUES
(1,'ANN'),
(2,'JAKE'),
(1,'JULIE'),
(3,'PAUL'),
(2,'SHANE'),
(4,'KUMI')

SELECT DISTINCT ID ,
STUFF((SELECT ','+NAME
FROM #A T1
WHERE T1.ID=T2.ID
FOR XML PATH('')),1,1,'') FROM #A T2
于 2016-07-29T09:15:24.770 回答
0

STUFF 不进行连接,它用于 XML 执行此操作。Stuff 仅根据选择替换值

select id,
stuff((select ','+name from #t1 t2 where t1.id=t2.id for xml path('')),1,1,'')
from #t1 t1
group by  id
于 2016-07-29T09:14:28.347 回答