如何在PIVOT中订购

时间:2014-05-09 02:11:48

标签: sql pivot

伙计我有一点问题 我希望通过枢轴中的id排序数据.. 你可以通过fiddle

试试

谢谢

1 个答案:

答案 0 :(得分:0)

问题似乎是在Person_Id声明中使用了COUNT()。以下作品:

SELECT Person_Id, Name, 
       [Honda], [Suzuki], [Yamaha], [Harley Davidson]
FROM (SELECT P.Name, PM.MotorCycle, PM.Person_id
      FROM Person P LEFT JOIN
           dbo.Person_MotorCycle PM
           ON P.Person_Id = PM.Person_Id    
     ) AS ST 
PIVOT (COUNT(MotorCycle)
       FOR MotorCycle IN
       ([Honda], [Suzuki], [Yamaha], [Harley Davidson]
       )
   ) PIV
ORDER BY Person_Id;