2012-03-21 110 views
0

你能幫我一個情況下,下面移調排列使用SQL Server

我有一個原始表,就像這樣:


+++++++++++++++++++++++++++ 
Col1 | Col2 
+++++++++++++++++++++++++++ 
A | 1 
A | 2 
A | 3 
B | 4 
B | 5 
 

那麼我想結果是這樣的:


+++++++++++++++++++++++++++ 
Col1 | Col2 
+++++++++++++++++++++++++++ 
A | 1,2,3 
B | 4,5 
 

我應該如何在SQL Server中執行此操作?

+3

讓我們來看看您的嘗試... – 2012-03-21 02:53:32

回答

1
select distinct t.col1, (
SELECT STUFF(
(
    SELECT ',' + convert(varchar(10),col2) 
    FROM TABLE 
    where col1 = t.col1 
    FOR XML PATH('') 
), 1, 1, '') 
) col2 
from TABLE t 
+0

您好,Lwin Oo,非常感謝。 – FirmanHidayat 2012-03-21 03:20:16

+0

它的工作。優秀 – FirmanHidayat 2012-03-21 03:31:23