员工的SQL加班

时间:2018-10-11 03:02:08

标签: sql-server

我正在尝试编写一个代码,使该员工即使先前已拒绝加班交易,也可以为其OT进行另一笔交易。

Employee_1 Shift: 9:00:00-18:00:00   OT Start Time: 18:00:00

场景1  例如:

**Employee**: Employee_1    **Time:** 18:00:00-22:00:00    **Status:**  Rejected

**Employee:** Employee_1    **Time:** 18:00:00-20:00:00            
**Status:** (This should be allowed since previous transaction is rejected)

场景2

**Employee:** Employee_1  **Time:**18:00:00-21:00:00   **Status:** Authorized

**Employee:**Employee_1   **Time:** 18:00:00-20:00:00  **[Status:][1]** (Overlapping not allowed) 



if exists(select 'X'   from Employee_OT_TBL tbl (nolock)            
where   tbl.ot_plan_date = @ot_plan_date          
and @ot_from_datetime < tbl.ot_to_datetime
and @ot_todatetime > tbl.ot_from_datetime 
and status = case when tbl.ot_status = 'REJ' then 0 else 1 end
and status = case when ot.doc_status = 'PEND' then 0 else 1 end ) 

员工OT开始时间OT结束时间状态 员工_1 18:00:00 21:00:00已授权 员工_1 18:00:00 20:00:00不允许重叠

员工OT开始时间OT结束时间状态 员工_1 18:00:00 21:00:00拒绝 Employee_1 18:00:00 20:00:00进行授权(自从以前的申请获得授权以来,这是允许的

1 个答案:

答案 0 :(得分:0)

我最近从事SQL for overlapping time periods

的工作

请检查所引用的文档以详细了解解决方案

我将为您建议以下SQL代码。

您可以删除CASE语句和[与上一页重叠]列。

但是需要NewStatus字段CASE。它检查是否发生重叠,然后使用SQL Lag function

读取前一个数据行的状态

在使用UPDATE语句之前,请先测试查询

select
 *,
 case when
  (ot_start between (lag(ot_start,1) over (partition by employee order by ot_start)) and (lag(ot_end,1) over (partition by employee order by ot_start))) or
  (ot_end between (lag(ot_start,1) over (partition by employee order by ot_start)) and (lag(ot_end,1) over (partition by employee order by ot_start))) or
  (ot_start < (lag(ot_start,1) over (partition by employee order by ot_start)) and ot_end > (lag(ot_end,1) over (partition by employee order by ot_start))) or
  (ot_start >(lag(ot_start,1) over (partition by employee order by ot_start)) and ot_end < (lag(ot_end,1) over (partition by employee order by ot_start)))
 then 
    'yes'
 when (lag(ot_start,1) over (partition by employee order by ot_start)) is null
 then NULL
 else 'no'
 end as [OverLapping with Previous],

 case when
  (ot_start between (lag(ot_start,1) over (partition by employee order by ot_start)) and (lag(ot_end,1) over (partition by employee order by ot_start))) or
  (ot_end between (lag(ot_start,1) over (partition by employee order by ot_start)) and (lag(ot_end,1) over (partition by employee order by ot_start))) or
  (ot_start < (lag(ot_start,1) over (partition by employee order by ot_start)) and ot_end > (lag(ot_end,1) over (partition by employee order by ot_start))) or
  (ot_start >(lag(ot_start,1) over (partition by employee order by ot_start)) and ot_end < (lag(ot_end,1) over (partition by employee order by ot_start)))
 then 
    case when  (lag([Status],1) over (partition by employee order by ot_start)) = 'Rejected' then 'Approved' else 'Rejected' end
 when (lag(ot_start,1) over (partition by employee order by ot_start)) is null
 then [Status]
 else [Status]
 end as [NewStatus]
from overtime 

我只做了有限的测试。这是输出

enter image description here