※SQL Server 2016にて確認
-- ダミーテーブル
with dummyData as (
select 'AGroup' as code, 'A01' as child
union
select 'AGroup' , 'A02'
union
select 'AGroup' , 'A03'
union
select 'BGroup' , 'B01'
union
select 'BGroup' , 'B02'
union
select 'BGroup' , 'B03'
union
select 'CGroup' , 'C01'
)
select
code,
left(hoge.child, len(hoge.child) - 1) as child --末尾のセミコロンを除く
from
(
select distinct
dummy.code,
(
select
dummyData.child + ';' AS [text()] --セミコロン区切りしたい列を指定
from
dummyData
where
dummyData.code = dummy.code --大元のfrom句で指定されているテーブルとJOIN
for xml path ('')
) as child
from
dummyData dummy
) hoge
■実行結果
code | value |
AGroup | A01;A02;A03 |
BGroup | B01;B02;B03 |
CGroup | C01 |