如图所示,表t1,select 11月12日的打卡记录。
打卡规则:必须在08:00以前刷一次卡,以及必须在17:00以后刷一次卡。
我想得到的结果:
如果两次打卡07:33,17:33。那么则不显示异常数据。
如果两次打卡07:33,16:00,那么显示一条数据16:00,新增一列为早退。

解决方案 »

  1.   

    with a as
    (
    select empid, recdate, min(rectime) t1 from tb where rectime<'12:00' group by empid, recdate  -- 上午有效数据
    ), b as
    (
    select empid, recdate, min(rectime) t2 from tb where rectime>='12:00' group by empid, recdate --下午有效数据
    ), c as
    (
    select isnull(a.empid,b.empid) empid, isnull(a.recdate,b.recdate) recdate, t1, t2
    from a full join b on a.emplid=b.emplid and a.recdate=b.recdate
    )
    select * from c where isnull(t1,'12:00')>'08:00' or isnull(t2,'12:00')<'17:00'
    -- 显示问题就是case when了,组合太多我不一一帮你枚举。
    另外要根据排班表才能找出旷工(没打卡)数据