3
我試圖連接兩個表,然後分析出的結果爲單獨列像這樣:如何將SQL JOIN的結果顯示爲單獨的列?
表1:
Customer_ID
----------
1
2
3
表2:
Customer_ID ListID
------------------
1 1
1 2
1 5
2 1
2 3
期望的結果:
Customer_ID ListID1 ListID2 ListID3
-----------------------------------
1 1 2 5
2 1 3
3
我用一個LEFT JOIN來組合表和一個GROUP BY與同Custmer_ID組列如下所示:
SELECT MIN([Table1].[Customer_ID])
,MIN([Table2].[ListID])
FROM [Table1]
LEFT JOIN [Table2]
ON [Table2].[Customer_ID] = [Table1].[Customer_ID]
GROUP BY [Table1].[Customer_ID]
當前結果:
Customer_ID ListID
------------------
1 1
2 1
3 NULL
我想不通的地方,從這裏去解析ListID的成單獨的列。有沒有辦法迭代ListID的?
請看看透視功能http://stackoverflow.com/questions/14797691/dynamic-pivot-columns-in-sql-server – Hituptony