我有一张表说EmployeeAbsence有三列:EmployeeId,LeaveReason和Date。该表记录了员工每天的出勤情况。我想知道过去14天休假的员工名单。
例如,
1 XYZ 2009-07-20
2 XYZ 2009-07-19
--
---
--
1001 XYZ 2009-07-04
在这种情况下,我的查询输出应该返回XYZ,因为它包含连续14天缺席的员工的条目。我很感激ORACLE查询。
答案 0 :(得分:1)
您的查询需要两个约束
对于约束(1),您需要知道从日期B减去日期A会导致这两个日期之间的天数。
对于约束(2),您需要按员工ID分组。
那说
SELECT EmployeeID
FROM EmployeeAbsence
WHERE Date between SYSDATE - 14 and SYSDATE
GROUP BY EmployeeId
应该这样做。
答案 1 :(得分:0)
我认为该表每天缺席1条记录,而您不希望检索缺席的员工。上个月,但在过去14天内返回。
SELECT employeeId
FROM employeeAbsences base
WHERE date > trunc(sysdate)-15 -- we want to include one more day for HAVING clause to work with
GROUP BY employeeId
-- had 2 or more non-consecutive leaves --> at least one of them started during last 14 days
HAVING count(*) < max(date) - min(date)
-- first absence is less than 14 days ago
OR min(date) > trunc(sysdate) - 14;