【发布时间】:2020-04-11 08:54:39
【问题描述】:
代码:
DROP TABLE IF EXISTS #LookupDates ;
CREATE TABLE #LookupDates (StartDate DATETIME, EndDate DATETIME)
INSERT INTO #LookupDates
VALUES
( '2019-12-01 08:00:00', '2019-12-01 16:00:00' )
, ( '2019-12-02 10:00:00', '2019-12-02 18:00:00' )
, ( '2019-12-03 08:30:00', '2019-12-03 16:30:00' )
, ( '2019-12-04 08:00:00', '2019-12-04 16:00:00' )
, ( '2019-12-05 08:00:00', '2019-12-05 16:00:00' )
, ( '2019-12-06 09:00:00', '2019-12-06 17:00:00' )
, ( '2019-12-07 08:30:00', '2019-12-07 16:30:00' )
, ( '2019-12-08 11:00:00', '2019-12-08 15:30:00' )
, ( '2019-12-09 08:30:00', '2019-12-09 16:30:00' )
, ( '2019-12-10 10:30:00', '2019-12-10 16:45:00' )
DROP TABLE IF EXISTS #Data ;
CREATE TABLE #Data (EmpId INT, ClockInDate DATETIME, ClockOutDate DATETIME)
INSERT INTO #Data
VALUES
( 1, '2019-12-01 07:00:00', '2019-12-01 07:30:00' ) -- Completely before
, ( 1, '2019-12-01 18:00:00', '2019-12-01 22:00:00' ) -- Completely after
, ( 1, '2019-12-02 09:30:00', '2019-12-02 18:00:00' ) -- Clockin before
, ( 1, '2019-12-03 09:00:00', '2019-12-03 16:30:00' ) -- Clockin after
, ( 1, '2019-12-04 08:00:00', '2019-12-04 15:45:00' ) -- Clockout before
, ( 1, '2019-12-05 08:00:00', '2019-12-05 17:15:00' ) -- Clockout after
, ( 1, '2019-12-06 08:40:00', '2019-12-06 16:45:00' ) -- Clockin before & Clockout before
, ( 1, '2019-12-07 08:25:00', '2019-12-07 17:05:00' ) -- Clockin before & Clockout after
, ( 1, '2019-12-08 12:00:00', '2019-12-08 15:15:00' ) -- Clockin after & Clockout before
, ( 1, '2019-12-09 08:30:01', '2019-12-09 16:30:27' ) -- Clockin after & Clockout before
, ( 1, '2019-12-10 10:30:00', '2019-12-10 16:45:00' ) -- Clockin on-time & Clockout on-time
描述:
LookupDates 包含公司的每日营业时间。实际表有额外天数的数据。
数据包含每个员工的时钟输入/输出时间戳。实际表有额外的 emps 和 days 数据。
目标:查找#Data 中的条目是否超出当天的营业时间。
- 员工打卡早吗?多少秒?
- 员工打卡迟到了吗?多少秒?
- emp 提前下班了吗?多少秒?
- emp 打卡晚了吗?多少秒?
期望的输出:
EmpId LookupStartDate ClockInDate IsClockInBefore ClockInBeforeTimeInSec IsClockInAfter ClockInAfterTimeInSec LookupEndDate ClockOutDate IsClockOutBefore ClockOutBeforeTimeInSec IsClockOutAfter ClockOutAfterTimeInSec
1 '2019-12-01 08:00:00' '2019-12-01 07:00:00' 1 3600 0 0 '2019-12-01 16:00:00' '2019-12-01 07:30:00' 1 30600 0 0
1 '2019-12-01 08:00:00' '2019-12-01 18:00:00' 0 0 1 36000 '2019-12-01 16:00:00' '2019-12-01 22:00:00' 0 0 1 21600
1 '2019-12-02 10:00:00' '2019-12-02 09:30:00' 1 1800 0 0 '2019-12-02 18:00:00' '2019-12-02 18:00:00' 0 0 0 0
1 '2019-12-03 08:30:00' '2019-12-03 09:00:00' 0 0 1 1800 '2019-12-03 16:30:00' '2019-12-03 16:30:00' 0 0 0 0
1 '2019-12-04 08:00:00' '2019-12-04 08:00:00' 0 0 0 0 '2019-12-04 16:00:00' '2019-12-04 15:45:00' 1 900 0 0
1 '2019-12-05 08:00:00' '2019-12-05 08:00:00' 0 0 0 0 '2019-12-05 16:00:00' '2019-12-05 17:15:00' 0 0 1 4500
1 '2019-12-06 09:00:00' '2019-12-06 08:40:00' 1 1200 0 0 '2019-12-06 17:00:00' '2019-12-06 16:45:00' 1 900 0 0
1 '2019-12-07 08:30:00' '2019-12-07 08:25:00' 1 300 0 0 '2019-12-07 16:30:00' '2019-12-07 17:05:00' 0 0 1 2100
1 '2019-12-08 11:00:00' '2019-12-08 12:00:00' 0 0 1 3600 '2019-12-08 15:30:00' '2019-12-08 15:15:00' 1 900 0 0
1 '2019-12-09 08:30:00' '2019-12-09 08:30:01' 0 0 1 1 '2019-12-09 16:30:00' '2019-12-09 16:30:27' 0 0 1 27
1 '2019-12-10 10:30:00' '2019-12-10 10:30:00' 0 0 0 0 '2019-12-10 16:45:00' '2019-12-10 16:45:00' 0 0 0 0
【问题讨论】:
-
因此您只需加入日期部分即可。例如,演员表上的内连接 b(a.LookupStartDate 作为日期)=演员表(b.ClockInDate 作为日期)?但我觉得这个“问题”可能比你描述的更多。
-
很好地设置数据。做得好,谢谢。但是,缺少的是您解决问题的尝试,以及您遇到问题的指示。我们不是来为您编写代码的。我们随时为您解决遇到的问题提供帮助。
-
您省略了您尝试的内容和一个问题。看起来您想加入
date,计算重叠(如果有),然后使用case表达式生成一些bit列。
标签: sql sql-server tsql date sql-server-2016