数据库:sqlserver 2008
现在我有一张表
里面的字段如下
clock_id,emp_id,sign_time
1         1      2010-11-20 09:00:00
1         2      2010-11-20 09:00:00
1         2      2010-11-21 09:00:00
2         1      2010-11-20 18:00:00
2         2      2010-11-21 18:00:00
1         1      2010-11-21 09:00:00
其中clock_id是我的考勤机的编号,emp_id是工号,sign_time 是打卡的时间
现在我有2个表,第一个考勤机是记录的进门时间,第二个考勤机是出门时间。
我想查出每人每天第一次的打卡时间,最后一次打卡的时间

解决方案 »

  1.   

    select emp_id , 
           convert(varchar(10),sign_time,120) sign_time,
           min(sign_time) 第一次的打卡时间,
           max(sign_time) 最后一次打卡的时间
    from tb
    group by  emp_id , convert(varchar(10),sign_time,120)
      

  2.   

    select emp_id ,  
      convert(varchar(10),sign_time,120) sign_time,
      min(sign_time) 第一次的打卡时间,
      max(sign_time) 最后一次打卡的时间
    from tb
    group by emp_id , convert(varchar(10),sign_time,120)
      

  3.   

    楼上的  肯定不行的
    第一次打卡的时间是 clock_id =1的
    最后一次打开时间是 clock_id =2的
    你说的那种我都写出来了···
      

  4.   

    select emp_id ,  
      convert(varchar(10),sign_time,120) sign_time,
      min(sign_time) 第一次的打卡时间,
      max(sign_time) 最后一次打卡的时间
    from ( --先合并结果集
    select clock_id,emp_id,sign_time=进门时间 from tbB
    union all
    select clock_id,emp_id,sign_time=出门时间 from tbB
    ) t
    group by emp_id , convert(varchar(10),sign_time,120)
      

  5.   

     楼上的大哥  我现在的数据 就是在一张表里 不需要 合并了  我汗··
    因为 我的 Clock_id 在后面有用 所以我这里才特意把 clock_id加了进去
      

  6.   

    不好意思 我重新把题目写一下数据库:sqlserver 2008
    现在我有一张表
    里面的字段如下
    clock_id,emp_id,sign_time
    1 1 2010-11-20 09:00:00
    1 2 2010-11-20 09:00:00
    1 2 2010-11-21 09:00:00
    2 1 2010-11-20 18:00:00
    2 2 2010-11-21 18:00:00
    1 1 2010-11-21 09:00:00
    其中clock_id是我的考勤机的编号,emp_id是工号,sign_time 是打卡的时间
    现在我有2个考勤机,第一个考勤机是记录的进门时间,第二个考勤机是出门时间。
    我想查出每人每天第一次的打卡时间,最后一次打卡的时间进门的考勤机的clock_id=1;出门的clock_id=2
      

  7.   

    select t.emp_id,
           convert(varchar(10),t.sign_time,120) sign_time,
           第一次的打卡时间 = (select min(sign_time) from tb where emp_id = t.emp_id and clock_id = 1 and datediff(day,sign_time,t.sign_time) = 0),
           最后一次打卡的时间 = (select max(sign_time) from tb where emp_id = t.emp_id and clock_id = 2 and datediff(day,sign_time,t.sign_time) = 0)
    from tb t
    group by t.emp_id,convert(varchar(10),t.sign_time,120)
      

  8.   


    --create test data
    IF OBJECT_ID('tb') IS NOT NULL DROP TABLE tb
    GO
    CREATE TABLE tb
    (
        clock_id int,
        emp_id int,
        sign_time DATETIME
    )
    insert into tb
    select 1,1,'2010-11-20 09:00:00' union all
    select 1,2,'2010-11-20 09:00:00' union all
    select 1,2,'2010-11-21 09:00:00' union all
    select 2,1,'2010-11-20 18:00:00' union all
    select 2,2,'2010-11-21 18:00:00' union all
    select 1,1,'2010-11-21 09:00:00'select * from tb--sql
    select emp_id ,  
      convert(varchar(10),sign_time,120) sign_time,
      min(sign_time) 第一次的打卡时间,
      max(sign_time) 最后一次打卡的时间
    from (
    select emp_id,sign_time from tb
    ) t
    group by emp_id , convert(varchar(10),sign_time,120)--result
    emp_id sign_time 第一次的打卡时间 最后一次打卡的时间
    --------------------------------------------------------------------
    1 2010-11-20 2010-11-20 09:00:00.000 2010-11-20 18:00:00.000
    2 2010-11-20 2010-11-20 09:00:00.000 2010-11-20 09:00:00.000
    1 2010-11-21 2010-11-21 09:00:00.000 2010-11-21 09:00:00.000
    2 2010-11-21 2010-11-21 09:00:00.000 2010-11-21 18:00:00.000
      

  9.   

    create table tb(clock_id int,emp_id int,sign_time datetime)
    insert into tb values(1 ,1 ,'2010-11-20 09:00:00')
    insert into tb values(1 ,2 ,'2010-11-20 09:00:00')
    insert into tb values(1 ,2 ,'2010-11-21 09:00:00')
    insert into tb values(2 ,1 ,'2010-11-20 18:00:00')
    insert into tb values(2 ,2 ,'2010-11-21 18:00:00')
    insert into tb values(1 ,1 ,'2010-11-21 09:00:00')
    goselect isnull(m.emp_id,n.emp_id) emp_id,isnull(m.sign_time,n.sign_time) sign_time,
           m.min_time , n.max_time from
    (
    select emp_id ,  
      convert(varchar(10),sign_time,120) sign_time,
      min(sign_time) min_time
    from tb where clock_id = 1
    group by emp_id , convert(varchar(10),sign_time,120)
    ) m
    full join
    (
    select emp_id ,  
      convert(varchar(10),sign_time,120) sign_time,
      max(sign_time) max_time
    from tb where clock_id = 2
    group by emp_id , convert(varchar(10),sign_time,120)
    ) n
    on m.emp_id = n.emp_id and m.sign_time = n.sign_timedrop table tb/*
    emp_id      sign_time  min_time                                               max_time                                               
    ----------- ---------- ------------------------------------------------------ ------------------------------------------------------ 
    1           2010-11-20 2010-11-20 09:00:00.000                                2010-11-20 18:00:00.000
    1           2010-11-21 2010-11-21 09:00:00.000                                NULL
    2           2010-11-20 2010-11-20 09:00:00.000                                NULL
    2           2010-11-21 2010-11-21 09:00:00.000                                2010-11-21 18:00:00.000(所影响的行数为 4 行)
    */