SQL数据透视查询数据

时间:2016-03-08 14:37:13

标签: sql sql-server tsql pivot

有人可以告诉我查询。我有一张表格,其中包含以下数据,我希望转发这些数据。

(^\/en\/|=en$)

我想将其转换为如下所示:

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

1 个答案:

答案 0 :(得分:0)

使用MAX代替COUNT

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