SELECT
  Y.ClientProductFeeID,
  Y.StartDate,
  Y.SalesData,
  Y.CancelData,
  Y.ReactivationData,
  Y.ConvertOldData,
  Y.ConvertNewData,
  Y.IncreaseData,
  Y.DecreaseData,
  Y.AutoData,
  (Y.SalesData + Y.CancelData + Y.ReactivationData + Y.ConvertOldData + Y.ConvertNewData + Y.IncreaseData + Y.DecreaseData + Y.AutoData) AS Growth,
  Y.ReallocateData,
  (Y.SalesData + Y.CancelData + Y.ReactivationData + Y.ConvertOldData + Y.ConvertNewData + Y.IncreaseData + Y.DecreaseData + Y.AutoData + Y.ReallocateData) AS TotalGrowth,
  RetainerFee,
  '' AS Action
FROM (

  SELECT
    PF.ClientProductFeeID,
    PF.StartDate,
    IF ((PF.EntryType = 'N'),                            PF.Difference, 0) AS SalesData,
    IF ((PF.EntryType = 'C') OR ((PF.EntryType = 'S') AND (PF.Difference < 0)), PF.Difference, 0) AS CancelData,
    IF (((PF.EntryType = 'S') AND (PF.Difference >= 0)), PF.Difference, 0) AS ReactivationData,
    IF ((PF.EntryType = 'D'),                            PF.Difference, 0) AS ConvertOldData,
    IF ((PF.EntryType = 'P'),                            PF.Difference, 0) AS ConvertNewData,
    IF (((PF.EntryType = 'M') AND (PF.Difference >= 0)), PF.Difference, 0) AS IncreaseData,
    IF (((PF.EntryType = 'M') AND (PF.Difference < 0)),  PF.Difference, 0) AS DecreaseData,
    IF ((PF.EntryType = 'R'),                            PF.Difference, 0) AS ReallocateData,
    IF ((PF.EntryType = 'A'),                            PF.Difference, 0) AS AutoData,
    RetainerFee,
    '' AS Action
  FROM (

      SELECT
        B.ClientProductFeeID,
        B.ClientBranchProductID,
        B.StartDate,
        B.EntryType,
        C.RetainerFee AS PreviousRetainerFee,
        IF(C.RetainerFee IS NULL, B.RetainerFee, (B.RetainerFee - C.RetainerFee)) AS Difference,
        B.RetainerFee
      FROM
        taClientProductFees AS B
          LEFT JOIN (
            SELECT
              A.ClientProductFeeID,
              ( SELECT MAX(M.ClientProductFeeID)
                FROM taClientProductFees AS M
                WHERE
                  (M.ClientBranchProductID = A.ClientBranchProductID) AND
                  (M.ClientProductFeeID <> A.ClientProductFeeID) AND
                  (M.StartDate = (

                    SELECT MAX(N.StartDate)
                    FROM taClientProductFees AS N
                    WHERE
                      (N.ClientBranchProductID = A.ClientBranchProductID) AND
                      (
                        (N.StartDate < A.StartDate) OR
                        ((N.StartDate = A.StartDate) AND (N.ClientProductFeeID < A.ClientProductFeeID))
                      )

                  ))
              ) AS PreviousID
            FROM
              taClientProductFees AS A
          ) AS X USING (ClientProductFeeID)
          LEFT JOIN taClientProductFees AS C ON (C.ClientProductFeeID = X.PreviousID)
      WHERE
        (B.ClientBranchProductID = @nClientBranchProductID)

    ) AS PF
  WHERE
    (PF.ClientBranchProductID = @nClientBranchProductID)

) AS Y
ORDER BY
  Y.StartDate,
  Y.ClientProductFeeID
