简体   繁体   English

Oracle SQL中的连续记录

[英]Consecutive records in oracle SQL

I have a similar question thought to post here. 我有一个类似的问题想在这里发布。 Need Employees who were absent for 3 or more consecutive days. 需要连续三天或以上缺勤的员工。 Assume all the days are working days no holidays on Saturday/Sunday/Anyother days. 假设所有的工作日都是周六/周日/其他任何一天的假日。

In the table below I need 20342123 and 20311111 in the result set. 在下表中,我需要在结果集中添加20342123和20311111。 Employee 20311333 has taken 4 leaves but they are not consecutive. 员工20311333已休4假,但不连续。

Thanks for help in advance. 预先感谢您的帮助。

Emp Id     |  AsofDate      | Comment 
----------------------------------
20342123   |  1-JAN-2017| Absent 
20342123   |  2-JAN-2017| Absent 
20342123   |  3-JAN-2017| Absent
20311111   |  1-JAN-2017| Absent 
20311111   |  2-JAN-2017| Absent 
20311111   |  3-JAN-2017| Absent
20311333   |  5-JAN-2017| Absent
20311333   |  6-JAN-2017| Absent
20311333   |  8-JAN-2017| Absent 
20311333   |  9-JAN-2017| Absent
20322222   |  1-JAN-2017| Absent
20322222   |  2-JAN-2017| Absent

Although you can apply a "gaps-and-islands" solution, I think lag() / lead() is simpler for this problem: 尽管您可以应用“缺口和孤岛”解决方案,但我认为lag() / lead()对于此问题更简单:

select distinct emp_id
from (select t.*,
             lead(date) over (partition by empid order by date) as date_1,
             lead(date, 2) over (partition by empid order by date) as date_2
      from t
      where comment = 'Absent'  -- not sure if this is important
     ) t
where date_1 = date + 1 and date_2 = date + 2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM