我有一张名为“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
我有一张名为“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
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)
或者
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]