select ID,
Name =
(
stuff
(
(
select ',' +
Name from Table_1 where ID =
a.ID for xml path('')
),1,1,''
)
) from Table_1 a group by ID
原始数据:
ID Name
A S
A M
A L
B S
B M
B L
结果数据:
ID Name
A S,M,L
B S,M,L
select ID,
Name =
(
stuff
(
(
select ',' +
Name from Table_1 where ID =
a.ID for xml path('')
),1,1,''
)
) from Table_1 a group by ID
原始数据:
ID Name
A S
A M
A L
B S
B M
B L
结果数据:
ID Name
A S,M,L
B S,M,L