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 天或更長間隔的員工。