SELECT
  UT.TravelRateID,
  UT.UserID,
  U1.EmployeeCode,
  U1.FullName,
  BR.BranchName,
  U1.VehiclePurchasePrice,
  fnDepreciatedVehicleValue(U1.VehiclePurchasePrice, U1.VehiclePurchaseDate, @aEffectiveDate, @nAnnualVehicleDepreciation) AS DepreciatedVehicleValue,
  UT.DateChanged,
  UT.TravelRate
FROM
  taUsers AS U1
LEFT JOIN taUsersTravelRates AS UT ON (UT.UserID = U1.UserID)
LEFT JOIN (
  SELECT
    UserID,
    fnUserBranch(UserID, @aEffectiveDate) AS BranchID
  FROM
    taUsers
  WHERE
    (Status = 1) AND
    (AllowTravel = 1)
) AS U2 ON (U2.UserID = U1.UserID)
LEFT JOIN taBranches AS BR ON (BR.BranchID = U2.BranchID)
WHERE
  (U1.Status = 1) AND
  (U1.AllowTravel = 1) AND
  (UT.TravelRateID = (

        SELECT MAX(X.TravelRateID) AS TravelRateID
        FROM taUsersTravelRates AS X
        WHERE
          (X.UserID = UT.UserID) AND
          (X.DateChanged = (
            SELECT MAX(Y.DateChanged)
            FROM taUsersTravelRates AS Y
            WHERE
              (Y.UserID = UT.UserID) AND
              (Y.DateChanged <= @aEffectiveDate)
          ))

  ))
  @pBranchID
  @pConsultantID
ORDER BY
  U1.FullName,
  UT.DateChanged