获取连续11天没有打卡的员工
P粉103739566
2023-08-18 09:31:19
<p>我正在尝试从数据库中获取连续11天没有标记出勤的员工,
为此我有员工表和出勤表,但是我在这方面遇到的问题是出勤表中没有记录,所以我该如何获取</p>
<p>我尝试了很多查询,其中一些如下:</p>
<pre class="brush:php;toolbar:false;">SELECT e.name, e.full_name
FROM tabEmployee e
LEFT JOIN (
SELECT
employee,
MIN(attendance_date) AS first_attendance_date
FROM tabAttendance
GROUP BY employee
) ar ON e.name = ar.employee
WHERE ar.first_attendance_date IS NULL OR
NOT EXISTS (
SELECT 1
FROM tabAttendance
WHERE employee = e.name
AND attendance_date >= ar.first_attendance_date
AND attendance_date < DATE_ADD(ar.first_attendance_date, INTERVAL 11 DAY)
)</pre>
<p>另一个查询:</p>
<pre class="brush:php;toolbar:false;">SELECT e.name, COUNT(a.`attendance_date`), COUNT(e.`name`) from tabEmployee e
LEFT JOIN tabAttendance a ON e.name = a.`employee`
WHERE a.`employee` IS NULL
GROUP BY e.`name`</pre>
<p><br /></p>
使用
LAG()
分析函数,我们可以尝试:这里的基本策略是在 CTE 中生成出勤日期的 lag(前一个连续值)。然后我们只报告有 11 天或更长间隔的员工。