INSERT INTO ttCrossSiloConsultantBranches (ConsultantID, ConsultantClientBranchID)
SELECT DISTINCT
  BP.ConsultantID,
  BP.ClientBranchID AS ConsultantClientBranchID
FROM
  taClientBranchProducts AS BP
    INNER JOIN taClientProducts AS CP ON (CP.ClientProductID = BP.ClientProductID)
    INNER JOIN taProducts AS PR ON (PR.ProductID = CP.ProductID)
WHERE
  (CP.Status = 1) AND
  (BP.Status = 1) AND
  (BP.JoinDate <= @aToDate) AND
  ((BP.TerminationDate IS NULL) OR (BP.TerminationDate > @aToDate)) AND
  (PR.ProductCategory IN ('HEALTH AND SAFETY', 'HR', 'IR', 'PAYROLL', 'TRAINING')) AND
  (BP.ConsultantID <> 0) AND
  (CP.ProductID <> 14)

  @pConsultantID
