如果计划列与

时间:2016-02-10 17:55:07

标签: sql-server

我有2个表计划表和LoginTime。计划有colStart和colStop。 LoginTime有colLogin和colLogout。我有一个SQL命令,声明如果colLogin不= colStart,那么它将放置一个' 1'第3个表的新列中的值。但是我需要添加几分钟的缓冲,所以如果colLogin说上午6:04并且colStart是6:00 AM,那么它将在第三列和表中报告0。有没有办法确定这个垫子?

以下代码我也使用了不同的变体:

SELECT loginlogout.[Name], loginlogout.[Login Time]
,      case 
          when loginlogout.[Login Time] = Schedules$.StartTime then 0 
          when loginlogout.[Login Time] BETWEEN '5:56AM' AND '6:05AM' then 0
          when loginlogout.[Login Time] BETWEEN '6:06AM' and '6:29AM' then 1
          when loginlogout.[Login Time] BETWEEN '6:30AM' and '6:34AM' then 0
          when loginlogout.[Login Time] BETWEEN '6:35AM' and '6:55AM' then 1
          when loginlogout.[Login Time] BETWEEN '6:56AM' AND '7:05AM' then 0
          when loginlogout.[Login Time] BETWEEN '7:06AM' AND '7:29AM' then 1
          when loginlogout.[Login Time] BETWEEN '7:30AM' and '7:34AM' then 0
          when loginlogout.[Login Time] BETWEEN '7:35AM' and '7:55AM' then 1
          when loginlogout.[Login Time] BETWEEN '7:56AM' AND '8:05AM' then 0
          when loginlogout.[Login Time] BETWEEN '8:06AM' AND '8:29AM' then 1
          when loginlogout.[Login Time] BETWEEN '8:30AM' and '8:34AM' then 0
          when loginlogout.[Login Time] BETWEEN '8:35AM' and '8:55AM' then 1
          when loginlogout.[Login Time] BETWEEN '8:56AM' and '9:05AM' then 0
          when loginlogout.[Login Time] BETWEEN '9:06AM' and '9:29AM' then 1
          when loginlogout.[Login Time] BETWEEN '9:30AM' and '9:34AM' then 0
          when loginlogout.[Login Time] BETWEEN '9:35AM' and '9:55AM' then 1
          when loginlogout.[Login Time] BETWEEN '9:56AM' and '10:05AM' then 0
          when loginlogout.[Login Time] BETWEEN '10:06AM' and '10:29AM' then 1
          when loginlogout.[Login Time] BETWEEN '10:30AM' and '10:34AM' then 0
          when loginlogout.[Login Time] BETWEEN '10:35AM' and '10:55AM' then 1
          when loginlogout.[Login Time] BETWEEN '10:56AM' and '11:05AM' then 0
          when loginlogout.[Login Time] BETWEEN '11:06AM' and '11:29AM' then 1
          else 1
       end as LoginInc 
into LoginIncidents 
FROM loginlogout 
JOIN Schedules$ 
  on loginlogout.[Name] = Schedules$.Name

然而,我了解到如果有人计划在上午6:00开始并在上午6:32登录,那么因为我有when loginlogout.[Login Time] BETWEEN '6:30AM' and '6:34AM' then 0,所以不会发生任何事件。有什么建议?

1 个答案:

答案 0 :(得分:2)

使用datediff并检查分钟数。

select
  case 
    when abs(datediff(minute, login, start)) between 0 and 5 then 1 
    else 0 
  end

如果您想让负差异与积极差异相同,请移除上面的abs()来电。

select
  case 
    when datediff(minute, login, start) between -4 and 5 then 1 
    else 0 
  end