SELECT
  Y.BranchName,
  Y.Consultant,
  Y.ClientCountA,
  Y.NonAttCountA,
  ROUND((Y.NonAttCountA / Y.ClientCountA * 100), 0) AS PercentageA,
  Y.ClientCountB,
  Y.NonAttCountB,
  ROUND((Y.NonAttCountB / Y.ClientCountB * 100), 0) AS PercentageB,
  Y.ClientCountC,
  Y.NonAttCountC,
  ROUND((Y.NonAttCountC / Y.ClientCountC * 100), 0) AS PercentageC,
  Y.ClientCountD,
  Y.NonAttCountD,
  ROUND((Y.NonAttCountD / Y.ClientCountD * 100), 0) AS PercentageD,
  (Y.ClientCountA + Y.ClientCountB + Y.ClientCountC + Y.ClientCountD) AS ClientCountTotal,
  (Y.NonAttCountA + Y.NonAttCountB + Y.NonAttCountC + Y.NonAttCountD) AS NonAttCountTotal,
  ROUND(((Y.NonAttCountA + Y.NonAttCountB + Y.NonAttCountC + Y.NonAttCountD) /
    (Y.ClientCountA + Y.ClientCountB + Y.ClientCountC + Y.ClientCountD) * 100), 0) AS PercentageTotal
FROM (

  SELECT
    Z.LabourNetBranchName AS BranchName,
    Z.Consultant,
    SUM(IF(Z.ClientGroup = 'A', 1, 0)) AS ClientCountA,
    SUM(IF(Z.ClientGroup = 'A', IF(((Z.LastAttendanceDate < @aFromDate) OR (Z.LastAttendanceDate IS NULL)) AND (Z.JoinDate < @aFromDate), 1, 0), 0)) AS NonAttCountA,
    SUM(IF(Z.ClientGroup = 'B', 1, 0)) AS ClientCountB,
    SUM(IF(Z.ClientGroup = 'B', IF(((Z.LastAttendanceDate < @aFromDate) OR (Z.LastAttendanceDate IS NULL)) AND (Z.JoinDate < @aFromDate), 1, 0), 0)) AS NonAttCountB,
    SUM(IF(Z.ClientGroup = 'C', 1, 0)) AS ClientCountC,
    SUM(IF(Z.ClientGroup = 'C', IF(((Z.LastAttendanceDate < @aFromDate) OR (Z.LastAttendanceDate IS NULL)) AND (Z.JoinDate < @aFromDate), 1, 0), 0)) AS NonAttCountC,
    SUM(IF(Z.ClientGroup = 'D', 1, 0)) AS ClientCountD,
    SUM(IF(Z.ClientGroup = 'D', IF(((Z.LastAttendanceDate < @aFromDate) OR (Z.LastAttendanceDate IS NULL)) AND (Z.JoinDate < @aFromDate), 1, 0), 0)) AS NonAttCountD
  FROM (

    @pInnerSQL

  ) AS Z
  GROUP BY
    1,2

) AS Y
WHERE
  ((Y.ClientCountA + Y.ClientCountB + Y.ClientCountC + Y.ClientCountD) > 0)