「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]