1.查找每个人每天最早和最晚的记录,2.并用最早和最晚的记录,与"09:00:00"和"18:00:00"做比较,计算出迟到或早退的分钟数.3.最后统计每个人每个月迟到和早退的次数和总的分钟数.
 ID              姓名         编号     日期时间                签到/退
1549             陈佳         306      2006-12-20 12:39:00     上班签到
1550             陈佳         306      2006-12-20 12:40:00     上班签到
1551             陈佳         306      2006-12-20 13:42:00     上班签到
1552             陈佳         306      2006-12-20 15:40:00     下班签退
1553             孙小勇       503      2006-12-13 10:05:00     下班签退
1554             孙小勇       503      2006-12-20 11:46:00     上班签到
1555             孙小勇       503      2006-12-20 12:46:00     上班签到
1556             孙小勇       503      2006-12-20 12:43:00     上班签到
1557             孙小勇       503      2006-12-20 15:38:00     下班签退
1558             李琳         516      2006-12-6 14:43:00      上班签到
1559             李琳         516      2006-12-6 14:43:00      上班签到
1560             李琳         516      2006-12-6 14:43:00      上班签到
1561             李琳         516      2006-12-6 14:43:00      上班签到
1562             李琳         516      2006-12-7 12:13:00      上班签到

解决方案 »

  1.   

    最早时间:
    select * from table1 where not exists (select id from table1 where id = a.id and 日期时间 < a.日期时间) as a最晚时间:
    select * from table1 where not exists(select * from table1 where id = a.id and 日期时间 > a.日期时间) as a
      

  2.   

    2.迟到分钟数:
    select 姓名,DATEDIFF(minute,cast((convert(char(10),日期时间,120) + ' 09:00:00') as datetime),日期时间) as 迟到分钟 from table1 where not exists (select id from table1 where 姓名 = a.姓名 and 日期时间 < a.日期时间) as a早退分钟:
    select 姓名,DATEDIFF(minute,日期时间,cast((convert(char(10),日期时间,120) + ' 18:00:00') as datetime),) as 迟到分钟 from table1 where not exists(select * from table1 where 姓名 = a.姓名 and 日期时间 > a.日期时间) as a
      

  3.   

    一楼的写法有问题:1.
    最早时间:
    select * from table1 where not exists (select id from table1 where 姓名 = a.姓名 and 日期时间 < a.日期时间) as a最晚时间:
    select * from table1 where not exists(select * from table1 where 姓名 = a.姓名 and 日期时间 > a.日期时间) as a
      

  4.   

    怎么没有人说用 min max