Question

Guys I Have A Little Problem I Want sort data by id in pivot.. you can try this through fiddle

thanks

Was it helpful?

Solution

The problem appears to be the use of Person_Id in the COUNT() statement. The following works:

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top