2014-12-08 37 views
0

我試圖從行的數據轉換爲多個連接的字符串轉換行值連接字符串

HH 3(6.27) 
HH 4(4.48) 
LH 5(0) 
HH 6(2.27) 
HH 7(0) 
LL 31(0) 
LM 32(0) 

這是子查詢的一個結果,我該如何將它轉換爲

HH 3(6.27), 4(4.48), 6(2.27), 7(0) 
LH 5(0) 
LL 31(0) 
LM 32(0) 

回答

2

您正在尋找聚合字符串連接。這裏是你如何做到這一點在SQL Server中:

with cte(col1, col2) as (
     < your subquery here > 
    ) 
select distinct col1, 
     stuff((select ', ' + col2 
       from cte cte2 
       where ct2.col1 = cte.col1 
       for xml path ('') 
      ), 1, 2, '') as col2s 
from cte;