SELECT  selectCondition
FROM (
SELECT CONVERT( CONCAT( date(le.from_date), 'to', date(le.to_date) ) USING latin1 ) AS leave_period , lt.name AS `type` , ROUND(le.no_of_days,2) AS entitlement , 
SUM(IF(l.date between le.from_date and le.to_date, IF( l.status =3, ROUND(l.length_days,2), 0), 0)) AS taken_days,
SUM(IF(l.date between le.from_date and le.to_date, IF(l.status = 2, ROUND(l.length_days,2), 0), 0)) AS scheduled_days,
IF(let.name = 'Brought Forward', ROUND(le.no_of_days,2),0) AS `brought_forward`,
(IF(let.name = 'Added', ROUND(le.no_of_days,2),0) + IF(let.name = 'Accrual', ROUND(le.no_of_days,2),0) + IF(let.name = 'Brought Forward', ROUND(le.no_of_days,2),0)) AS `total`
FROM ohrm_leave l
LEFT JOIN ohrm_leave_entitlement le ON l.emp_number = le.emp_number and le.leave_type_id = l.leave_type_id
LEFT JOIN ohrm_leave_type lt ON lt.id = le.leave_type_id
LEFT JOIN ohrm_leave_entitlement_type let ON let.id =   le.entitlement_type 
WHERE  whereCondition1
GROUP BY lt.id,`leave_period`
ORDER BY `leave_period` DESC
) AS `report_records`