我想通过上下班时间统计出某人每月上班迟到的次数,9点开始上班,starttime,endtime都为datetime类型,例如
    name         starttime             endtime       
    lily       2009-1-1 9:00:00    2009-1-1 9:10:00
请问该如何实现?哪位大侠若知道方法,麻烦告诉一下,不甚感激。

解决方案 »

  1.   

    题目没看的不是很明白..那个starttime,endtime都是什么日期啊, 上下班日期???
      

  2.   

    starttime为上班时间,endtime为下班时间,它们的数据类型是datetime类型
      

  3.   

    select 
      name,
      sum(case when datepart(hh,endtime)>9 then 1 else 0 end) as 迟到次数
    from 
      tb
    group by
      name
      

  4.   

    这样??
    -- =============================================
    -- 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)*/
      

  5.   

    非常感谢大家的帮助,4楼的方法我试了,如果上班时间是9:10:00的话就不算迟到了。可能我没有说清楚给大家带来麻烦。
    是这样的,公司规定早上9:00:00开始上班,18:00:00下班。如果某员工上班时刷卡时间在
    9:00:00以后就算迟到,下班刷卡时间在18:00:00以前就算早退。starttime为员工上班刷卡时间,endtime为下班刷卡时间。我想通过这两个字段来统计出某员工是否迟到以及迟到的次数。麻烦各位了,谢谢。
      

  6.   

    select count(*) from 表名 where starttime between (上班时间,下班时间) or endtime between (上班时间,下班时间)
      

  7.   

    SELECT DATETIME(HOUR,STARTTME,ENDTIME)  取时差,
    SELECT DATEDIFF(MINUTE,GETDATE(),'2009-11-26 12:00') 取分差。
    像是你要加入条件来判断午休时间。
    这就是我的建议,
      

  8.   

    如果是统计是否迟到,写一个函数,取上班时间,用CONVERT(NVARCHAR(12),GETDATE(),120)取到当前日期。假如这个函数叫GETSTANDARDTIME()。可以用SELECT * FROM TABLE WHERE DATEDIFF(MINUTE,GETSTANDRDTIME(),STARTTIME)<0  这个就是迟到的,当然,你也可以用前台程序来处理标准上班时间。
    上一条回复的函数也是DATEDIFF,我写错了,抱歉