我想通过上下班时间统计出某人每月上班迟到的次数,9点开始上班,starttime,endtime都为datetime类型,例如
name starttime endtime
lily 2009-1-1 9:00:00 2009-1-1 9:10:00
请问该如何实现?哪位大侠若知道方法,麻烦告诉一下,不甚感激。
name starttime endtime
lily 2009-1-1 9:00:00 2009-1-1 9:10:00
请问该如何实现?哪位大侠若知道方法,麻烦告诉一下,不甚感激。
name,
sum(case when datepart(hh,endtime)>9 then 1 else 0 end) as 迟到次数
from
tb
group by
name
-- =============================================
-- Author: T.O.P
-- Create date: 2009/11/26
-- Version: SQL SERVER 2005
-- =============================================
declare @TB table([name] varchar(4),[starttime] datetime,[endtime] datetime,[wtime] datetime)
insert @TB
select 'lily','2009-1-1 9:00:00','2009-1-1 9:10:00','2009-1-1 9:02:00' union all
select 'lily','2009-1-1 9:00:00','2009-1-1 9:10:00','2009-1-1 9:02:00' union all
select 'lily','2009-1-1 9:00:00','2009-1-1 9:10:00','2009-1-1 8:02:00' union all
select 'lily','2009-1-1 9:00:00','2009-1-1 9:10:00','2009-1-1 8:02:00' union all
select 'lily','2009-1-1 9:00:00','2009-1-1 9:10:00','2009-1-1 8:02:00' union all
select 'lily','2009-1-1 9:00:00','2009-1-1 9:10:00','2009-1-1 9:02:00'select count(*) as 迟到次数
from @TB
where wtime between starttime and [endtime]
--测试结果:
/*
迟到次数
-----------
3(1 row(s) affected)*/
是这样的,公司规定早上9:00:00开始上班,18:00:00下班。如果某员工上班时刷卡时间在
9:00:00以后就算迟到,下班刷卡时间在18:00:00以前就算早退。starttime为员工上班刷卡时间,endtime为下班刷卡时间。我想通过这两个字段来统计出某员工是否迟到以及迟到的次数。麻烦各位了,谢谢。
SELECT DATEDIFF(MINUTE,GETDATE(),'2009-11-26 12:00') 取分差。
像是你要加入条件来判断午休时间。
这就是我的建议,
上一条回复的函数也是DATEDIFF,我写错了,抱歉