日期:2014-05-17 浏览次数:20490 次
WITH temp (name, department)
AS (SELECT 'aaaa',
'a班'
UNION ALL
SELECT 'bbbb',
'a班'
UNION ALL
SELECT 'cccc',
'b班'
UNION ALL
SELECT 'dddd',
'c班')
SELECT department,
Stuff((SELECT ',' + CONVERT(VARCHAR(4), name)
FROM temp
WHERE department = A.department
ORDER BY department
FOR xml path('')), 1, 1, '') AS name
FROM temp A
GROUP BY department
/*
department name
---------- ---
a班 aaaa,bbbb
b班 cccc
c班 dddd
(3 行受影响)
*/