2013-10-01 41 views
0

我在我的SQLSERVER數據庫如下表:我怎麼UNPIVOT或拼合此表

FiguresYear FiguresMonth Apple Orange Banana Grape 
    2012  Jan   10 12  15  20 
    2013  Jan   1  2  3  5 

我想運行一個查詢返回的格式如下:

FiguresYear FiguresMonth FruitName FruitValue 
2012  Jan   Apple  10 
2012  Jan   Orange 12 
2012  Jan   Banana 15 
2012  Jan   Grape  20 
2013  Jan   Apple  1 
2013  Jan   Orange 2 
2013  Jan   Banana 3 
2013  Jan   Grape  5 

我想使用unpivot函數,但不能讓它工作。有沒有人知道如何做或無unpivot?

回答

0

像這樣:

SELECT * 
FROM tablename AS t 
unpivot 
(
    FruitValue 
    FOR FruitName IN([Apple], [Orange], [Banana], [Grape]) 
) AS u;