选择连续休假5天或以上的员工

fkaflof6  于 2021-08-01  发布在  Java
关注(0)|答案(2)|浏览(390)

我需要选择 staffId (员工),连续5天或更长时间的休假

select * from dbo.EmployeeRecords where reasonId = 3 and isPaid = 1
``` `reasonId = 3` 意味着假期
![](https://i.stack.imgur.com/cy8o4.jpg)
我该怎么做?
k3fezbri

k3fezbri1#

最简单的方法是 lead() --假设你每天只有不超过一行,并且没有间隔 workday 列。所以,要获得这样的假期的第一天:

select er.*
from (select er.*,
             lead(workday, 4) over (partition by staffid order by workday) as workday_4
      from dbo.EmployeeRecords er
      where reasonId = 3 and isPaid = 1
     ) er
where workday_4 = dateadd(day, 4, workday);

“4”比“5”小一个,因为当前日期已计为一天。
如果你中间有周末,需要考虑这些,那么5天就是一周。您可以使用:

where workday_4 <= dateadd(day, 6, workday);

因为5天等于一周,所以这很容易奏效。否则你需要考虑到一周中的哪一天。

jdg4fx2g

jdg4fx2g2#

使用带有count运算符的子查询尝试下面的查询

select * from dbo.EmployeeRecords 
where staffid in 
(
    select staffid 
        from 
        (
        select staffid, count(staffid) cnt rownum
        from dbo.EmployeeRecords 
        where reasonId = 3 and isPaid = 1
        group by staffid
        )t
        where t.cnt>=5
)

相关问题