SELECT e.id, e.first_name, e.middle_name, e.last_name, e.address, e.status, e.project_status, d.designation_name, p.project_name, j.site_name, u.id AS user_id, r.role_code FROM employee e LEFT JOIN users u ON ( u.employee_id = e.id ) LEFT JOIN roles r ON ( r.id = u.role_id ) LEFT JOIN designations d ON ( e.designation_id = d.id ) LEFT JOIN designation_tasks dt_ ON ( dt_.designation_id = d.id AND dt_.status = 1 ) LEFT JOIN employees_projects ep ON ( e.id = ep.employee_id AND ep.status = 1 ) LEFT JOIN project p ON ( ep.project_id = p.id AND p.deleted = 0 ) LEFT JOIN job_site j ON ( j.id = p.site_id AND j.deleted = 0 ) LEFT JOIN tasks t ON ( dt_.task_id = t.id AND dt_.status = 1 ) WHERE e.id NOT IN(SELECT u.employee_id FROM users u LEFT JOIN employee e ON ( e.id = u.employee_id ) INNER JOIN roles r ON ( u.role_id = r.id AND r.role_code = 'ADMIN' )) AND ep.project_id IN (SELECT eps.project_id FROM employees_projects eps INNER JOIN employee es ON ( es.id = eps.employee_id AND eps.status = 1 ) INNER JOIN users us ON ( es.id = us.employee_id ) WHERE us.id = 180 AND es.status = 1) AND e.project_status = 1 AND (e.first_name || e.middle_name || e.last_name LIKE '%dEC%' OR e.first_name || e.middle_name LIKE '%dEC%') AND ( e.is_terminated IS NULL OR e.is_terminated = 0 ) GROUP BY e.id, j.id, p.id