2016-03-08 162 views
-2

有人可以告訴我查詢。我有一張桌子,裏面有我想要旋轉的下列數據。SQL數據透視查詢數據

Name Ride Hour 
Abbas Falls 1 
Abbas Falls 2 
Abbas Falls 3 
Shiraz Falls 1 
Shiraz Falls 2 
Shiraz Falls 3 
Adnan Falls 1 
Adnan Falls 2 
Adnan Falls 3 
Adnan Falls 4 
Abbas Lunch 4 
Shiraz Lunch 4 
Adnan Lunch 5 
Abbas Jolly 5 
Shiraz Jolly 5 
Abbas Jolly 6 
Shiraz Jolly 6 
Adnan Jolly 6 

我想將它轉化成這個樣子:

Name 1  2  3  4  5  6 
Abbas falls falls falls Lunch Jolly Jolly 
Shiraz falls falls falls Lunch Jolly Jolly 
Adnan falls falls falls falls Lunch Jolly 

我寫了這一點,但它並沒有做的工作

SELECT * 
FROM 
(SELECT * 
    FROM _Test) as test 
PIVOT 
(
COUNT(ride) 
FOR hour IN ([1], [2], [3], [4], [5], [6]) 
) AS PivotTable 

回答

0

使用MAX,而不是COUNT

SELECT * 
FROM (SELECT * 
     FROM Test) as test 
PIVOT (
    MAX(ride) 
    FOR hour IN ([1], [2], [3], [4], [5], [6])) AS PivotTable 
+0

對不起,這是一個滑倒,我糾正了它。再次感謝你 – Abbaskhan