1

表结构:

人员配备记录

  • PersonnelId int
  • GroupId 整数
  • StaffingStartDateTime 日期时间
  • StaffingEndDateTime 日期时间

在给定员工所属的日期和组 ID 的情况下,我如何获得人员配备记录列表,其中当前员工的数量在一天中的任何一分钟都低于阈值,例如 3?

我的大脑的工作方式是,我会在一天中的每一分钟重复调用一个存储过程,但这当然会非常低效:

SELECT COUNT(PersonnelId) 
FROM DailyRosters 
WHERE GroupId=@GroupId 
    AND StaffingStartTime <= @TimeParam 
    AND StaffingEndTime > @TimeParam 
    AND COUNT(GroupId) < 3
GROUP BY GroupId
HAVING COUNT(PersonnelId) < 3

编辑:如果有助于细化问题,员工可能会全天来来去去。例如,人员的人员配备记录可能从 0800 到 0815,另一个从 1000 到 1045。

4

2 回答 2

1

这是一个解决方案,我在其中找到所有不同的开始和结束时间,然后查询以查看当时有多少其他人打卡。每次答案小于 4 时,您就知道当时人手不足,并且大概要等到 NEXT 开始时间。

with meaningfulDtms(meaningfulTime, timeType, group_id)
as
(  
    select distinct StaffingStartTime , 'start' as timeType, group_id
    from DailyRosters 
    union
    select distinct StaffingEndTime , 'end' as timeType, group_id
    from DailyRosters 
)

select COUNT(*), meaningfulDtms.group_id, meaningfulDtms.meaningfulTime
from DailyRosters  dr
inner join meaningfulDtms on dr.group_id = meaningfulDtms.group_id
and (
 (dr.StaffingStartTime  < meaningfulDtms.meaningfulTime
     and dr.StaffingEndTime  >= meaningfulDtms.meaningfulTime
     and meaningfulDtms.timeType = 'start')
 OR
 (dr.StaffingStartTime  <= meaningfulDtms.meaningfulTime
     and dr.StaffingEndTime  > meaningfulDtms.meaningfulTime
     and meaningfulDtms.timeType = 'end')
)
group by meaningfulDtms.group_id, meaningfulDtms.meaningfulTime
having COUNT(*) < 4
于 2015-02-19T19:21:14.543 回答
0

使用 dt 在 PK 创建一个包含一天中所有分钟的表
它将有 1440 行

这不会给你零计数 - 没有工作人员

select allMiuntes.dt, worktime.grpID, count(distinct(worktime.personID))
  from allMinutes 
  join worktime 
    on allMiuntes.dt > worktime.start 
   and allMiuntes.dt < worktime.end 
 group by allMiuntes.dt, worktime.grpID 
having count(distinct(worktime.personID)) < 3

对于零次我认为最好的方法是掌握 grpID
但我不确定这个

select allMiuntes.dt, grpMaster.grpID, count(distinct(worktime.personID))
  from grpMaster
 cross join allMinutes 
  left join worktime 
    on allMiuntes.dt > worktime.start 
   and allMiuntes.dt < worktime.end 
   and worktime.grpID = grpMaster.grpID 
 group by allMiuntes.dt, grpMaster.grpID
having count(distinct(worktime.personID)) < 3
于 2015-02-19T19:53:55.430 回答