我有一张名为“PERSON”的表
GROUP_ID PERSON_NAME
1001 ALEX
1002 MATHEW
1001 GEORGE
1002 THOMAS
1001 JAMES
我需要输出如下"|"
GROUP_ID PERSON_NAME
1001 ALEX|GEORGE|JAMES
1002 THOMAS|MATHEW
答案 0 :(得分:0)
SELECT a.[GROUP_ID],
SUBSTRING(d.Names,1, LEN(d.Names) - 1) NamesList
FROM
(
SELECT DISTINCT [GROUP_ID]
FROM Table1
) a
CROSS APPLY
(
SELECT [PERSON_NAME] + '|'
FROM table1 AS B
WHERE A.[GROUP_ID] = B.[GROUP_ID]
FOR XML PATH('')
) D (Names)
OR
SELECT [GROUP_ID],
STUFF(
(SELECT '|' + [PERSON_NAME]
FROM Table1
WHERE [GROUP_ID] = a.[GROUP_ID]
FOR XML PATH (''))
, 1, 1, '') AS NamesList
FROM Table1 AS a
GROUP BY [GROUP_ID]