gpt4 book ai didi

sql - 如何让第一个员工在临近轮类时间时入场

转载 作者:行者123 更新时间:2023-12-03 00:44:21 25 4
gpt4 key购买 nike

我有一个 employee_attendance 表 (SQL Server 2012),其中包含以下列:

  • AttendanceId(身份)
  • 员工 ID
  • 时间戳(打卡或打卡时)
  • 访问代码(I = 输入,O = 输出)

这是带有一些示例数据的SQL Fiddle:http://sqlfiddle.com/#!3/ba8a1/1

我知道这些员工可以在我们 3 个轮类中的任何一天工作:

  1. A 类(07:00 至 15:00)
  2. B 类(15:00 至 23:00)
  3. C类(23:00至次日07:00)

So what I need is to know for each employee in a selected day (the sample data has just one day and has been filtered to show only IN records, so only IN access to the company) is the first Timestamp that is near (below or above) the Shift entrance.

下面是应用正确脚本后 SQL Fiddle 数据应该是什么样子的图像:

enter image description here

最佳答案

由于您拥有 SQL Server 2012,因此您可以使用 TimeFromParts 和“mod 8”一切来最大程度地减少工作...

select employeeId,
accessCode,
minDiff = MIN(ABS(DATEDIFF(TIMEFROMPARTS(DATEPART(HH,t.timestamp) % 8,
(DATEPART(MI,t.timestamp),
(DATEPART(S,t.timestamp),0,0),
TIMEFROMPARTS(7,
(DATEPART(MI,t.timestamp),
(DATEPART(S,t.timestamp))
from table t
were t.timestamp is in a given daily range

因此,由于您的类次更改(7,15,23)都是“mod 8 = 7”,我只需将时间戳“mod 8”的小时部分与“7”进行比较 - 这样我就不需要运行查询三次。

关于sql - 如何让第一个员工在临近轮类时间时入场,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30382508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com