原数据姓名           打卡时间
A         2008/8/20  17:15:00
A         2008/8/21  8:00:00
A         2008/8/21  12:00:00
B         2008/8/21  12:05:00
B         2008/8/21  07:55:00要求结果集
姓名      考勤日期     打卡时间       打开批次(以日期分组,按时间排序)
A         2008/8/20    17:15:00        第1次
A         2008/8/21    8:00:00         第1次
A         2008/8/21    12:00:00        第2次
B         2008/8/21    12:05:00        第2次
B         2008/8/21    07:55:00        第1次
谢谢  

解决方案 »

  1.   

    --> liangCK小梁 于2008-08-21
    --> 生成测试数据: #T
    if object_id('tempdb.dbo.#T') is not null drop table #T
    create table #T (姓名 varchar(1),打卡时间 datetime)
    insert into #T
    select 'A','2008/8/20 17:15:00' union all
    select 'A','2008/8/21 8:00:00' union all
    select 'A','2008/8/21 12:00:00' union all
    select 'B','2008/8/21 12:05:00' union all
    select 'B','2008/8/21 07:55:00'select 姓名,打卡时间,
           (select count(*) from #T where 姓名=t.姓名 
                     and convert(varchar(10),打卡时间,120)=convert(varchar(10),t.打卡时间,120)
                       and 打卡时间<=t.打卡时间) 打卡次数
    from #T t/*
    姓名   打卡时间                    打卡次数
    ---- ----------------------- -----------
    A    2008-08-20 17:15:00.000 1
    A    2008-08-21 08:00:00.000 1
    A    2008-08-21 12:00:00.000 2
    B    2008-08-21 12:05:00.000 2
    B    2008-08-21 07:55:00.000 1(5 行受影响)*/
      

  2.   

    select 姓名
    ,考勤日期=convert(varchar(10),打卡时间,120)
    ,打卡时间=convert(varchar(8),打卡时间,113)
    打卡批次=(select count(*) from t where 姓名=a.姓名
              and convert(varchar(10),打卡时间,120)
               = convert(varchar(10),a.打卡时间,120) 
             and 打卡时间<=a.打卡时间)
    from t a