2011-07-26 44 views
0

例四行:想連接3 - 在SQL Server

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
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] 
+0

令人印象深刻。我缺乏XML集成技能突然讓我感到羞愧。 –