非常感谢csw200201在上个帖子中的 的回答 耽误您不少时间了吧 非常谢谢 估计是我没有说清楚吧 现在我也考虑把表再简化一下 我把具体情况说一次 (我现在只统计表里面每天为偶数的记录的时间比如下面的记录,也不考虑进出的情况)
  ID   卡号        考勤时间
  1  2038375602 2010-03-08 9:25:05   
  2  2038375602 2010-03-08 10:25:05   
  3  2038375601 2010-03-08 10:28:05 
  4  2038375601 2010-03-08 12:28:05
  5  2038375601 2010-03-08 17:28:05  
  6  2038375602 2010-03-08 12:25:05  
  7  2038375602 2010-03-08 15:25:05  
  8  2038375603 2010-03-08 9:00:00   
  9  2038375603 2010-03-08 10:00:00  
  10 2038375603 2010-03-09 9:00:00   
  11 2038375603 2010-03-09 10:30:00   得到的结果 应该是 2038375602 2010-03-08 4
                2038375603 2010-03-08 1
                2038375602 2010-03-09 1.5
就是统计每天的 考勤小时数  考勤次数为奇数的 就不做统计  (我希望的结果是 写一个函数 传一个开始日期 和结束日期  就能自动统计出 这个时间段 每个卡号的 时间段内每天的考勤时间 )

解决方案 »

  1.   

    记得这个问题我上次也费了不少事,给你写得很清楚,而且包括刷卡次数有问题的
    并且逐个情况进行了测试,可是不知道您是否满意。
    还是用上次的 思路给你写一遍,如果不行你就说说,怎么改。
    ---1
    if object_id('tb') is not null drop table tb
    go
    create table tb(id int identity(1,1),人员卡号 varchar(20), 考勤时间 datetime )---2
    insert tb
    select '2038375602',' 2010-03-08 9:25:05' union all     
    select '2038375602',' 2010-03-08 10:25:05' union all     
    select '2038375601',' 2010-03-08 10:28:05' union all    
    select '2038375601',' 2010-03-08 12:28:05' union all  
    select '2038375601',' 2010-03-08 17:28:05' union all     
    select '2038375602',' 2010-03-08 12:25:05' union all     
    select '2038375602',' 2010-03-08 15:25:05' union all     
    select '2038375603',' 2010-03-08 9:00:00' union all     
    select '2038375603',' 2010-03-08 10:00:00' union all     
    select '2038375603',' 2010-03-09 9:00:00' union all     
    select '2038375603',' 2010-03-09 10:30:00'  alter function f_test(@s datetime,@e datetime)
    returns @t table(人员卡号 varchar(50), 考勤时间 datetime ,小时 numeric(4,1))
    as 
    begin
    declare @t1 table (rowid int,人员卡号 varchar(50), 考勤时间 datetime )
    insert into @t1
    select rowid=count(1),a.人员卡号 , a.考勤时间  
    from tb a left join tb b 
    on a.人员卡号=b.人员卡号 and a.考勤时间>=b.考勤时间 and datediff(dd,a.考勤时间,b.考勤时间)=0
    where a.考勤时间 between @s and @e
    group by a.人员卡号 , a.考勤时间
    order by a.人员卡号 , a.考勤时间

    insert into @t
    select 人员卡号=a.人员卡号 + case when max(a.rowid)%2=1 then '该卡刷'+ltrim(max(a.rowid))+'次,请查明问题!'else '' end,
    convert(varchar(10),a.考勤时间,120) 考勤时间,
    工作时间=sum(case when a.rowid%2=0 then datediff(mi,b.考勤时间,a.考勤时间) else datediff(mi,a.考勤时间,b.考勤时间)end)*1.0/60
    from @t1 a left join  @t1 b  
    on a.人员卡号=b.人员卡号 and a.rowid=b.rowid+1 and datediff(dd,a.考勤时间,b.考勤时间)=0
    group by a.人员卡号,convert(varchar(10),a.考勤时间,120)
    return
    endselect * from dbo.f_test('2010-03-01','2010-03-10')/*
    人员卡号                                               考勤时间                                                   小时     
    -------------------------------------------------- ------------------------------------------------------ ------ 
    2038375601该卡刷3次,请查明问题!                             2010-03-08 00:00:00.000                                -3.0
    2038375602                                         2010-03-08 00:00:00.000                                2.0
    2038375603                                         2010-03-08 00:00:00.000                                1.0
    2038375603                                         2010-03-09 00:00:00.000                                1.5(所影响的行数为 4 行)
    */
      

  2.   

       xys_777 非常谢谢你的解答,但是统计的不对   比如说
    2038375602 2010-03-08 应该是4个小时 9:25进去,10:25出来  12:25:05又进去了 15:25:05出来的  应该是 1小时 + 3小时    一共4个小时 
      

  3.   

      谢谢dawugui的提醒  加了这个就更复杂了 。 并且有时候进了没刷 多刷  很多情况 很难处理的
      

  4.   

    小小一改if object_id('tb') is not null drop table tb
    go
    create table tb(id int identity(1,1),人员卡号 varchar(20), 考勤时间 datetime )---2
    insert tb
    select '2038375602',' 2010-03-08 9:25:05' union all     
    select '2038375602',' 2010-03-08 10:25:05' union all     
    select '2038375601',' 2010-03-08 10:28:05' union all    
    select '2038375601',' 2010-03-08 12:28:05' union all  
    select '2038375601',' 2010-03-08 17:28:05' union all     
    select '2038375602',' 2010-03-08 12:25:05' union all     
    select '2038375602',' 2010-03-08 15:25:05' union all     
    select '2038375603',' 2010-03-08 9:00:00' union all     
    select '2038375603',' 2010-03-08 10:00:00' union all     
    select '2038375603',' 2010-03-09 9:00:00' union all     
    select '2038375603',' 2010-03-09 10:30:00'  alter function f_test(@s datetime,@e datetime)
    returns @t table(人员卡号 varchar(50), 考勤时间 datetime ,小时 numeric(4,1))
    as 
    begin
        declare @t1 table (rowid int,人员卡号 varchar(50), 考勤时间 datetime )
        insert into @t1
        select rowid=count(1),a.人员卡号 , a.考勤时间  
        from tb a left join tb b 
        on a.人员卡号=b.人员卡号 and a.考勤时间>=b.考勤时间 and datediff(dd,a.考勤时间,b.考勤时间)=0
        where a.考勤时间 between @s and @e
        group by a.人员卡号 , a.考勤时间
        order by a.人员卡号 , a.考勤时间
        
        insert into @t
        select 人员卡号=a.人员卡号 + case when max(a.rowid)%2=1 then '该卡刷'+ltrim(max(a.rowid))+'次,请查明问题!'else '' end,
        convert(varchar(10),a.考勤时间,120) 考勤时间,
        工作时间=sum(case when a.rowid%2=1 then datediff(mi,a.考勤时间,b.考勤时间) else 0 end)*1.0/60
        from @t1 a left join  @t1 b  
        on a.人员卡号=b.人员卡号 and a.rowid=b.rowid-1 and datediff(dd,a.考勤时间,b.考勤时间)=0
        group by a.人员卡号,convert(varchar(10),a.考勤时间,120)
        return
    endselect * from dbo.f_test('2010-03-01','2010-03-10')/*
    人员卡号                                               考勤时间                                                   小时     
    -------------------------------------------------- ------------------------------------------------------ ------ 
    2038375601该卡刷3次,请查明问题!                             2010-03-08 00:00:00.000                                2.0
    2038375602                                         2010-03-08 00:00:00.000                                4.0
    2038375603                                         2010-03-08 00:00:00.000                                1.0
    2038375603                                         2010-03-09 00:00:00.000                                1.5(所影响的行数为 4 行)*/
      

  5.   

    给你个sql 语句吧。sql="select 卡号,count(*) 考勤次数,substring(考勤时间,1,10) 日期 from table1 group by 卡号,substring(考勤时间,1,10) where 考勤时间>='"+st+"' and 考勤时间<='"+et+"'"其中 st是开始时间,et是结束时间
      

  6.   


    I have given up on her - unfortunately. This is not a difficult problem but she's not getting it.
      

  7.   


    I am probably a bit too harsh on her. It's very frustrating that when you provide perfectly good solution and the recipient still totally does not get it.Anyway, Angel, if you are desperate for help, send me a PM and leave me your msn ID.