SELECT COUNT(DISTINCT e.id) FROM employees e INNER JOIN (SELECT employee_id, MAX(scan_time) as last_scan FROM badge_logs WHERE DATE(scan_time) = CURDATE() GROUP BY employee_id) latest ON e.id = latest.employee_id INNER JOIN badge_logs bl ON e.id = bl.employee_id AND bl.scan_time = latest.last_scan WHERE bl.scan_type = 'in'