SELECT
  CT.CaseID,
  CA.ClientID,
  CS.FinNumber,
  CS.GroupName AS ClientName,
  CA.ExternalCaseID,
  IF(DATE_FORMAT(FROM_UNIXTIME(CT.Created),      '%Y-%m-%d') = '1970-01-01', '', DATE_FORMAT(FROM_UNIXTIME(CT.Created),      '%Y-%m-%d')) AS DateCreated,
  IF(DATE_FORMAT(FROM_UNIXTIME(CT.DateDiarised), '%Y-%m-%d') = '1970-01-01', '', DATE_FORMAT(FROM_UNIXTIME(CT.DateDiarised), '%Y-%m-%d')) AS DateDiarised,
  IF(DATE_FORMAT(FROM_UNIXTIME(CT.SetdownDate),  '%Y-%m-%d') = '1970-01-01', '', DATE_FORMAT(FROM_UNIXTIME(CT.SetdownDate),  '%Y-%m-%d')) AS SetdownDate,
  S1.OptionDescription AS ActionName,
  U1.FullName AS CreatedBy,
  U2.FullName AS ActionedBy
FROM
  taCaseAction AS CT
    LEFT JOIN taUsers AS U1 ON (U1.UserID = CT.CreatedByID)
    LEFT JOIN taUsers AS U2 ON (U2.UserID = CT.ActionedByID)
    LEFT JOIN taCase AS CA ON (CA.CaseID = CT.CaseID)
    LEFT JOIN taClients AS CS ON (CS.ClientID = CA.ClientID)
    LEFT JOIN taSystemOption AS S1 ON ((S1.OptionType = 'CASE-ACTION-TYPE') AND (S1.OptionID = CT.ActionID))
WHERE
  1
  @pCaseID
  @pClientID
  @pActionID
  @pActionedByID
  @pCreateFromTo
  @pDiarisedFromTo
  @pSetdownFromTo
  @pFilter
ORDER BY
  CT.Created