前几天有发过贴,后又朋友解答,但效率太慢,先求最优算法。要求,根据3个参数, @cha, @date1, @date2
得到yymmdd @date1,@date2 之间的数据,而且,在cardid相同的情况下,行与行之间的数据差大于
@cha,但是每个cardid 的第一条数据是需要保留的。
作用是:筛选重复打卡的考勤数据。为更好理解,原来的帖子网址为: 点击这里
谢谢、

解决方案 »

  1.   

    按原帖做的测试:
    create table #tablename(empid int,createtime time)
    insert into #tablename
    select 1,'8:20'
    union all
    select 1,'8:24'
    union all
    select 1,'12:00'
    union all
    select 1,'12:01'
    union all
    select 2,'8:30'
    union all
    select 2,'8:31'
    union all
    select 2,'12:00'
    union all
    select 2,'13:30'
    ---测试
    with TT as (select empid,createtime,ROW_NUMBER() over(PARTITION by empid order by createtime) as rowid from #tablename)
    select distinct a.empid,case when DateDiff(minute,a.createtime,b.createtime)<5 then a.createtime else b.createtime end as createtime
    from TT as a left join TT as b on a.rowid=b.rowid-1 and a.empid=b.empid
    where not b.createtime is null