我要制作考勤统计系统,基本需求如下: 
我查询出12月分所有员工在07:00-08:00和12:00-13:00和17:00-18:00的所有打卡记录 
select * from noteinf where convert(char(7),t_notetime,120)='2008-12'and 
(convert(char(10),t_notetime,108) between '07:00' and '08:00' 
or convert(char(10),t_notetime,108) between '12:00' and '13:00' 
or convert(char(10),t_notetime,108) between '17:00' and '18:00') 
但是在每个时间段,比如07:00-08:00可能同一个人有两条或两条以上考勤记录,那么我只查询出记录最早的那一条,请问这条查询语句应该怎么写?
这个问题昨天我已经问过,最后确定了查询语句是
select min(t_notetime),t_num from noteinf where convert(char(7),t_notetime,120)='2008-12' 
and convert(char(5),t_notetime,108) between '07:00' and '08:30'  group by t_num  
union select min(t_notetime),t_num from noteinf where convert(char(7),t_notetime,120)='2008-12' 
and convert(char(5),t_notetime,108) between '12:00' and '13:30' group by t_num  
union select min(t_notetime),t_num from noteinf where convert(char(10),t_notetime,120)='2008-12' 
and convert(char(5),t_notetime,108) between '17:00' and '18:00'  group by t_num
今天我进行测试的时候发现问题,因为我的数据库中的打卡记录的数据是2008-12-1 7:56:49这种格式的,所有用min(t_notetime)得出来的日期也取的最小值,就是说如果2008-12-1有数据的话,则不会读出2008-12-2那天的数据。但是我需要查询每天的考勤记录的较早的记录,所以估计还是需要首先对日期进行循环,一天一天的来判断,请问这条循环语句应该怎么写呢?只以2008年12月为例即可!

解决方案 »

  1.   

    select min(t_notetime),t_num,convert(char(10),t_notetime,120) from noteinf where convert(char(7),t_notetime,120)='2008-12' 
    and convert(char(5),t_notetime,108) between '07:00' and '08:30'  group by t_num,convert(char(10),t_notetime,120)  
    union 
    select min(t_notetime),t_num,convert(char(10),t_notetime,120) from noteinf where convert(char(7),t_notetime,120)='2008-12' 
    and convert(char(5),t_notetime,108) between '12:00' and '13:30' group by t_num,convert(char(10),t_notetime,120)  
    union 
    select min(t_notetime),t_num,convert(char(10),t_notetime,120) from noteinf where convert(char(10),t_notetime,120)='2008-12' 
    and convert(char(5),t_notetime,108) between '17:00' and '18:00'  group by t_num,convert(char(10),t_notetime,120) 
      

  2.   

    group by里把日期也加上去就好了~select min(t_notetime),t_num,convert(char(10),t_notetime,120) from noteinf 
    where convert(char(7),t_notetime,120)='2008-12' 
    and convert(char(5),t_notetime,108) between '07:00' and '08:30'  
    group by t_num,convert(char(10),t_notetime,120)  
    union 
    select min(t_notetime),t_num,convert(char(10),t_notetime,120) from noteinf 
    where convert(char(7),t_notetime,120)='2008-12' 
    and convert(char(5),t_notetime,108) between '12:00' and '13:30' 
    group by t_num,convert(char(10),t_notetime,120)  
    union 
    select min(t_notetime),t_num,convert(char(10),t_notetime,120) from noteinf 
    where convert(char(10),t_notetime,120)='2008-12' 
    and convert(char(5),t_notetime,108) between '17:00' and '18:00' 
    group by t_num,convert(char(10),t_notetime,120)