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;