SELECT
  PS.ProspectID,
  PS.ProspectName,
  PS.ProspectTypeID,
  PS.LabourNetBranchID,
  PS.DateCreated,
  PS.OriginID,
  PS.OriginDetail,
  X.QuotedAmount,
  COALESCE(X.ClosedCount, 0) AS ClosedProductCount,
  COALESCE(X.SignedCount, 0) AS SignedProductCount,
  PS.IntroducingClientID,
  PS.IndustryID,
  PS.SubIndustryID,
  PS.SetaID,
  PS.SalesReferrerID,
  PS.EstTurnOver,
  PS.TaxTypeID,
  PS.PaymentMethod,
  PS.CompanyRegNumber,
  PS.VATNumber,
  PS.ClientID,
  PS.ClientDate,
  PS.RequestDateTime,
  Y.NoOfAppointments,
  Y.LastAppointmentDate,
  PS.LastChangeDate
FROM
  taProspect AS PS
    LEFT JOIN (
      SELECT
        ProspectID,
        SUM(QuotedAmount) AS QuotedAmount,
        SUM(IF((SalesPhaseID IS NULL) OR (SalesPhaseID < 5), 0, 1)) AS ClosedCount,
        SUM(IF(SalesPhaseID = 5, 1, 0)) AS SignedCount
      FROM
        taProspectProduct
      WHERE
        (ProspectID = @nProspectID)
      GROUP BY
        ProspectID
    ) AS X USING (ProspectID)
    LEFT JOIN (
      SELECT
        ProspectID,
        COUNT(*) AS NoOfAppointments,
        MAX(AttendanceDate) AS LastAppointmentDate
      FROM
        taPresentation
      WHERE
        (ProspectID = @nProspectID)
      GROUP BY
        ProspectID
    ) AS Y USING (ProspectID)
 WHERE
  (ProspectID = @nProspectID)
