http://topic.csdn.net/u/20101021/17/86aa8d23-baa3-406c-97e4-dbf7f273e215.html
原数据结构为:
韩海民 QS0013
韩海民 QS0014
韩海民 QS0015
杨田 QS0013
杨田 QS0014
想要实现的结果为:
韩海民 QS0013 QS0014 QS0015
杨田 QS0013 QS0014
sql server 2005
if
object_id ( 'tempdb.dbo.#' ) is not null drop table
#
create
table #(a varchar ( 8 ), b varchar ( 8
))
insert
into
#
select
'' , 'QS0013'
union all
select
'' , 'QS0014'
union all
select
'' , 'QS0015'
union all
select
'' , 'QS0013'
union all
select
'' ,
'QS0014'
select
a , ltrim (( select ' ' + b from # where a = t . a for xml path ( '' ))) from # t group by
a
select
a , stuff (( select ' ' + b from # where a = t . a for xml path ( '' )), 1 , 1 , ''
)
from
# t group by
a