SELECT DISTINCT
  AP.CalendarID,
  AP.AppointmentID,
  IF(COALESCE(PC.InternalProduct, 0) > 0, 1, 0) AS InternalProduct
FROM
  tbAppointments AS AP
    LEFT JOIN taProducts AS PR USING (ProductID)
    LEFT JOIN taProductCategory AS PC USING (ProductCategory)
WHERE
  (AP.AttendanceDate >= @aStartDate) AND
  (AP.AttendanceDate <= @aEndDate) AND
  (AP.CalendarID IS NOT NULL) AND
  (AP.CalendarID > 0) AND
  (AP.ConsultantID IN (@nUserID))
ORDER BY
  AP.CalendarID
