P粉2310799762023-08-19 07:58:16
使用 LAG()
分析函数,我们可以尝试:
WITH cte AS ( SELECT e.name, e.full_name, LAG(a.attendance_date) OVER (PARTITION BY a.employee ORDER BY a.attendance_date) AS lag_attendance_date FROM tabAttendance a INNER JOIN tabEmployee e ON e.name = a.employee ) SELECT DISTINCT name, full_name FROM cte WHERE DATEDIFF(attendance_date, lag_attendance_date) > 11;
这里的基本策略是在 CTE 中生成出勤日期的 lag(前一个连续值)。然后我们只报告有 11 天或更长间隔的员工。