想实现一个考勤管理的查询,在每天的时间记录中只取第一条时间为有效的时间来与一个标准时间进行比较.试问在如此多的数据中,我这样才能把每天/人的第一条时间取出来呢?时间如下:
id         
208         8           2006-4-1 08:21:21       
209         3           2006-4-1 08:24:23       
210         11          2006-4-1 08:26:25       
211         12          2006-4-1 08:30:31       
212         3           2006-4-1 08:41:01       
213         8           2006-4-1 08:52:01       
214         3           2006-4-2 08:22:07       
215         8          2006-4-2 08:32:07       
216         12          2006-4-2 08:33:07       
217         11          2006-4-2 08:42:08       
218         11          2006-4-2 08:48:12       
219         12          2006-4-2 08:52:12       
220         3           2006-4-3 08:12:12       
221         8          2006-4-3 08:18:12       
222        8            2006-4-3 08:22:22       
223         3           2006-4-3 08:27:26       
224         11          2006-4-3 08:29:26       
225         12          2006-4-3 08:31:26       
226         3           2006-4-4 08:17:26       
227         8          2006-4-4 08:19:26       
228         3           2006-4-4 08:23:30       
229         11          2006-4-4 08:27:30       
230         12          2006-4-4 08:31:30       
231        11           2006-4-4 08:33:33       
232        12           2006-4-4 08:34:38       
233         3           2006-4-5 08:17:35       
234          8          2006-4-5 08:20:38       
235         11          2006-4-5 08:21:38       
236         12          2006-4-5 08:22:38       
237         11          2006-4-1 08:30:38 ...........................................
等一批数据,在经过查询过后,能够得到如下所示的一些数据出来
208         8           2006-4-1 08:21:21       
209         3           2006-4-1 08:24:23       
210         11          2006-4-1 08:26:25       
211         12          2006-4-1 08:30:31    214         3           2006-4-2 08:22:07       
215         8          2006-4-2 08:32:07       
216         12          2006-4-2 08:33:07       
217         11          2006-4-2 08:42:08       
     
220         3           2006-4-3 08:12:12       
221         8          2006-4-3 08:18:12       
224         11          2006-4-3 08:29:26       
225         12          2006-4-3 08:31:26  
     
226         3           2006-4-4 08:17:26       
227         8           2006-4-4 08:19:26       
229         11          2006-4-4 08:27:30       
230         12          2006-4-4 08:31:30       233         3           2006-4-5 08:17:35       
234          8          2006-4-5 08:20:38       
235         11          2006-4-5 08:21:38       
236         12          2006-4-5 08:22:38
该怎样进行查询呢?

解决方案 »

  1.   

    select Userid,convert(char(10),enterdate,126),min(enterdate)
    from table1
    group by Userid,convert(char(10),enterdate,126)
      

  2.   


    declare @t table(id int ,no1 int , time1 datetime)insert into @T
    select 208,         8,           '2006-4-1 08:21:21'       union   
    select 209,         3,           '2006-4-1 08:24:23'       union   
    select 210,         11,          '2006-4-1 08:26:25'       union
    select 211,         12,          '2006-4-1 08:30:31'       union
    select 212,         3,           '2006-4-1 08:41:01'       union
    select 213,         8,           '2006-4-1 08:52:01'       union
    select 214,         3,           '2006-4-2 08:22:07'       union
    select 215,         8,           '2006-4-2 08:32:07'       union
    select 216,         12,          '2006-4-2 08:33:07'       union
    select 217,         11,          '2006-4-2 08:42:08'       union
    select 218,         11,          '2006-4-2 08:48:12'       union
    select 219,         12,          '2006-4-2 08:52:12'       union
    select 220,         3,           '2006-4-3 08:12:12'       union 
    select 221,         8,           '2006-4-3 08:18:12'       union
    select 222,         8,           '2006-4-3 08:22:22'       union   
    select 223,         3,           '2006-4-3 08:27:26'       union
    select 224,         11,          '2006-4-3 08:29:26'       union
    select 225,         12,          '2006-4-3 08:31:26'       union
    select 226,         3,           '2006-4-4 08:17:26'       union
    select 227,         8,           '2006-4-4 08:19:26'       union
    select 228,         3,           '2006-4-4 08:23:30'       union  
    select 229,         11,          '2006-4-4 08:27:30'       union
    select 230,         12,          '2006-4-4 08:31:30'       union
    select 231,         11,          '2006-4-4 08:33:33'       union
    select 232,         12,          '2006-4-4 08:34:38'       union 
    select 233,         3,           '2006-4-5 08:17:35'       union
    select 234,         8,           '2006-4-5 08:20:38'       union
    select 235,         11,          '2006-4-5 08:21:38'       union
    select 236,         12,          '2006-4-5 08:22:38'       union
    select 237,         11,          '2006-4-1 08:30:38' 
    select * from @t 
    where time1 in
    (
    select  min(time1)
    from @t
    group by no1 ,convert(varchar(10),time1,112)
    )
      

  3.   

    id         员工编号      日期
    208         8           2006-4-1 08:21:21       
    209         3           2006-4-1 08:24:23       
    210         11          2006-4-1 08:26:25       select *
    from 表
    where   id in(select min(id) from 表 group by 员工编号, cast(year(日期) as varchar(4)+','+cast(month(日期) as varchar(2))+','+cast(day(日期) as varchar(2))  )