如何获得特定员工的所有进出时间?

时间:2017-12-09 09:25:46

标签: sql sql-server

鉴于表:

    id       time_stamp                  Access Type    
    1001    2017-09-05 09:35:00         IN
    1002    2017-09-05 11:00:00         IN
    1001    2017-09-05 12:00:00         OUT
    1002    2017-09-05 12:25:00          OUT
    1001    2017-09-05 13:00:00          IN
    1002    2017-09-05 14:00:00         IN
    1001    2017-09-05 17:00:00          OUT
    1002   2017-09-05 18:00:00         OUT

我在下面尝试了这个查询:

    select  *,       datediff(minute, first_in, last_out) as durationfrom    (
        select  id
        ,       min(case when [Access Type] = 'IN' then time_stamp end) as first_in
        ,       max(case when [Access Type] = 'OUT' then time_stamp end) as last_out
        ,       cast(min(time_stamp) as date) as date
        from    Table1
        group by
                id
        ,       cast(time_stamp as date)
        ) as SubQueriesMustBeNamed

当我尝试这个查询时,它只显示在最后一个出口。

期望的结果:

    id      check_in    check_out  totalhrs  check_in check_out totalhrs  date           
    1001     09:35       12:00      2:25       13:00    17:00     2:00    2013-09-05       
    1002     11:00       12:25      1:25       14:00    18:00     4:00    2013-09-05  

结果会像这样。任何人帮助我..提前致谢

1 个答案:

答案 0 :(得分:2)

SQL Fiddle

MS SQL Server 2014架构设置

CREATE TABLE Table1
    ([id] int, [time_stamp] datetime, [AccessType] varchar(3))
;

INSERT INTO Table1
    ([id], [time_stamp], [AccessType])
VALUES
    (1001, '2017-09-05 09:35:00', 'IN'),
    (1002, '2017-09-05 11:00:00', 'IN'),
    (1001, '2017-09-05 12:00:00', 'OUT'),
    (1002, '2017-09-05 12:25:00', 'OUT'),
    (1001, '2017-09-05 13:00:00', 'IN'),
    (1002, '2017-09-05 14:00:00', 'IN'),
    (1001, '2017-09-05 17:00:00', 'OUT'),
    (1002, '2017-09-05 18:00:00', 'OUT')
;

查询1

select
       id, cast(time_stamp as date) [date]
     , format(max(case when in_rank = 1 then time_stamp end),'HH:mm')         check_in_1
     , format(max(case when in_rank = 1 then next_timestamp end),'HH:mm')     check_out_1
     , format(max(case when in_rank = 1 then 
           dateadd(ss,datediff(ss,time_stamp,next_timestamp),0) end),'HH:mm') total_hrs_1
     , format(max(case when in_rank = 2 then time_stamp end),'HH:mm')         check_in_2
     , format(max(case when in_rank = 2 then next_timestamp end),'HH:mm')     check_out_2
     , format(max(case when in_rank = 2 then
           dateadd(ss,datediff(ss,time_stamp,next_timestamp),0) end),'HH:mm') total_hrs_2
from (
      select
            id, time_stamp, AccessType, next_timestamp, next_accesstype
          , dense_rank() over(partition by id, cast(time_stamp as date) order by time_stamp) in_rank
      from table1 t1
      outer apply (
          select top(1) t2.time_stamp, t2.AccessType
          from table1 t2
          where t1.id = t2.id and t1.AccessType <> t2.AccessType
          and cast(t1.time_stamp as date) = cast(t2.time_stamp as date)
          and t1.time_stamp < t2.time_stamp
          order by t2.time_stamp
          ) oa (next_timestamp, next_accesstype)
      where AccessType = 'IN'
     ) d
group by id, cast(time_stamp as date)

<强> Results

|   id |       date | check_in_1 | check_out_1 | total_hrs_1 | check_in_2 | check_out_2 | total_hrs_2 |
|------|------------|------------|-------------|-------------|------------|-------------|-------------|
| 1001 | 2017-09-05 |      09:35 |       12:00 |       02:25 |      13:00 |       17:00 |       04:00 |
| 1002 | 2017-09-05 |      11:00 |       12:25 |       01:25 |      14:00 |       18:00 |       04:00 |