SELECT DISTINCT
  IF(CSP.ClientID IS NULL, RS.FinNumber, CSP.FinNumber) AS ElementID
FROM
  taClientRetentionSummary AS RS
    LEFT JOIN taClients AS CS ON (CS.ClientID = RS.ClientID)
    LEFT JOIN taClients AS CSP ON (CSP.ClientID = CS.ParentClientID)
    LEFT JOIN (
      SELECT
        CS1.FinNumber,
        CS1.IndustryID,
        CS1.SubIndustryID,
        CB1.LabourNetBranchID
      FROM
        taClients AS CS1
          LEFT JOIN taClientBranches CB1 ON (CB1.ClientID = CS1.ClientID)
      WHERE
        (CB1.IsHeadOffice = 1) AND
        ((CB1.BranchStatus = 1) OR (CS1.ClientStatus = 0))
    ) AS HO ON (HO.FinNumber = RS.FinNumber)
WHERE
  (RS.YearMonth = @nYearMonth)

  @pLabourNetBranchID
  @pConsultantID
  @pProductID
  @pIndustryID

ORDER BY 1
