想在SQL Server中连接三到四行

时间:2011-07-26 07:30:50

标签: sql-server-2005

示例:

Table  
Account id, team_name_product

最初是这样的:

account id team_name_product
1  MCLO:Wyatt, Gregory (SYM, SER); 
1  MCR2:Garcia, Rebecca (CRE); 
1  MCR1:Gonzalez) 

在整个帐户ID中,我希望连接具有不同人员名称的不同团队的3行。

结果应如下所示:

Account ID,(MCLO:Wyatt, Gregory (SYM, SER); MCR2:Garcia, Rebecca (CRE); MCR1:Gonzalez)                                      

1 个答案:

答案 0 :(得分:1)

select Y1.[account id],
       stuff((select ' '+Y2.team_name_product 
              from YourTable as Y2
              where Y1.[account id] = Y2.[account id]
              for xml path(''), type).value('.', 'varchar(max)'), 1, 1, '') as team_name_products
from YourTable as Y1
group by Y1.[account id]