请高手帮忙解释统计时间间隔查询语句的执行过程
刚刚开始自学些SQL细节,遇到如下问题。
某表 记录员工打卡 出入时间,以下查询为显示员工休息时间。
我不能理解这句查询的过程,请哪位高手能否不吝为小弟解惑。
CREATE TABLE timeclock
(Employee varchar(30),
TimeIn smalldatetime,
TimeOut smalldatetime
)
INSERT timeclock VALUES('Pythia','07:31:34','12:04:01')
INSERT timeclock VALUES('Pythia','12:45:10','17:32:49')
INSERT timeclock VALUES('Dionysus','9:31:29','10:46:55')
INSERT timeclock VALUES('Dionysus','10:59:32','11:39:12')
INSERT timeclock VALUES('Dionysus','13:05:16','14:07:41')
INSERT timeclock VALUES('Dionysus','14:11:49','14:57:02')
INSERT timeclock VALUES('Dionysus','15:04:12','15:08:38')
INSERT timeclock VALUES('Dionysus','15:10:31','16:13:58')
INSERT timeclock VALUES('Dionysus','16:18:24','16:58:01')
SELECT t1.Employee,
DATEADD(mi,1,t1.TimeOut) AS StartOfLoafing,
DATEADD(mi,-1,t2.TimeIn) AS EndOfLoafing,
DATEDIFF(mi,t1.TimeOut,t2.TimeIn) AS LengthOfLoafing
FROM timeclock T1 JOIN timeclock T2 ON (t1.Employee=t2.Employee)
WHERE (DATEADD(mi,1,t1.TimeOut)=
(SELECT MAX(DATEADD(mi,1,t3.TimeOut))
FROM timeclock T3
WHERE (t3.Employee=t1.Employee)
AND (DATEADD(mi,1,t3.TimeOut) <= DATEADD(mi,-1,t2.TimeIn))))
/*
Employee StartOfLoafing EndOfLoafing LengthOfLoafing
----------- ---------------------- ---------------------- --------------
Pythia 1900-01-01 12:05:00 1900-01-01 12:44:00 41
Dionysus 1900-01-01 10:48:00 1900-01-01 10:59:00 13
Dionysus 1900-01-01 11:40:00 1900-01-01 13:04:00 86
Dionysus 1900-01-01 14:09:00 1900-01-01 14:11:00 4
Dionysus 1900-01-01 14:58:00 1900-01-01 15:03:00 7
Dionysus 1900-01-01 15:10:00 1900-01-01 15:10:00 2
Dionysus 1900-01-01 16:15:00 1900-01-01 16:17:00 4
*/
drop table Employee
------解决方案--------------------就是把前一条记录的out时间加1分钟开始到下一次刷卡in的时间减1分钟当休息时间
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------t3,t2是别名
自己与自己比较
打卡出去的时间加上一分钟要小于等于 打卡进入的时间减去一分钟