 select 'E', employee_id,  ( select name from   Employee_Details   where _id = X.employee_id) Employee,  worked_days as 'No of \n Days Worked', total_num_of_days as 'Total no of \n Working days', basic_salary as 'Basic Salary', hra 'HRA', da 'DA', conveyance 'Conveyance',  total_num_of_Days - worked_days as 'Total leave taken',  paid_leave 'Paid leave',  basic_salary/total_num_of_days*(total_num_of_Days - worked_days - paid_leave) as 'Salary \n Deducted' ,  OverTime 'Overtime \n (hours)' ,OverTime_Pay 'Overtime Pay' ,  basic_salary+ da+ hra + conveyance - (basic_salary/total_num_of_days*(total_num_of_Days - worked_days - paid_leave)) + OverTime_Pay as 'Total Salary' from ( SELECT s.Employee_Id,  sum(case when a.Working_Hours >0 and HalfDay =0 then 1  when a.Working_Hours >0 and HalfDay = 1 then 0.5 else 0 end ) as worked_days ,  case when E.date_of_joining  >= str_to_date('$fromDateTime','%Y-%m-%d') then datediff(str_to_date('$toDateTime','%Y-%m-%d'), date_of_joining) else  datediff(str_to_date('$toDateTime','%Y-%m-%d'), str_to_date('$fromDateTime','%Y-%m-%d')) end as total_num_of_days,  sum(a.Overtime) as OverTime, sum(a.OverTime) * s.OverTime_Pay  as OverTime_Pay,  s.Basic_Salary, s.HRA, s.DA , s.Conveyance, s.Monthly_Leave as paid_leave   FROM Attendance_Details a JOIN Employee_Details E ON a.Employee_Id = E._id  join Employee_Salary_Details s on a.Employee_Id = s.Employee_ID WHERE Working_Day >= str_to_date('$fromDateTime','%Y-%m-%d') and Working_Day <= str_to_date('$toDateTime','%Y-%m-%d') group by Employee_Id) X 