2011-07-26 5 views
0

例では4行:を3連結する - SQL Serverの

Table 
Account id, team_name_product 

当初はこのようなものです:アカウントIDアクロス

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

を、私はとの異なるチームを持つ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統合スキルの欠如は突然私を恥じています。 –

関連する問題