MS SQL server 2005:检查时间输入/输出(时移工作)

时间:2014-06-16 09:05:05

标签: sql-server

我的时间出勤数据库每行都有下表

ID_Emp  FullName    DateCheck   TimeIO
11  RONA    11/05/2014  8:16
11  RONA    11/05/2014  13:35
11  RONA    11/05/2014  17:23
11  RONA    11/05/2014  21:09
12  Elizabe 11/05/2014  14:06
12  Elizabe 11/05/2014  22:39
13  Jimmy   11/05/2014  8:00
13  Jimmy   11/05/2014  12:00

输出

ID_Emp  FullName    DateCheck   TimeIn  TimeOut Hours
11  RONA    11/05/2014  8:16    13:35   5:19
11  RONA    11/05/2014  17:23   21:09   3:46
12  Elizabe 11/05/2014  14:06   22:39   8:33
13  Jimmy   11/05/2014  8:00    17:12   9:12                

1 个答案:

答案 0 :(得分:0)

假设您可以使用前一天没有CheckIn Rank() over (Partition获取所需的链接,此处称为使用表变量排名 或者是临时的,然后通过t1.ID_EMP=t2.ID_EMP and t1.DateCheck=t2.DateCheck and t1.Ranking=t2.Ranking -1加入条件(t1.Ranking & 1) = 1

Declare @tab table (ID_EMP int,FullName varchar(50),dateCheck datetime,TimeIO datetime)
insert into @tab
select 11,  'RONA',    '11/05/2014',  '8:16'
UNION
select 11,  'RONA'    ,'11/05/2014','13:35'
UNION
select 11,  'RONA'    ,'11/05/2014','17:23'
UNION
select 11,  'RONA'    ,'11/05/2014','21:09'
UNION
select 12,  'Elizabe' ,'11/05/2014','14:06'
UNION
select 12,  'Elizabe' ,'11/05/2014','22:39'
UNION
select 13,  'Jimmy'   ,'11/05/2014','8:00'
UNION
select 13,  'Jimmy'   ,'11/05/2014','12:00'

Declare @tab2 table (ID_EMP int,FullName varchar(50),dateCheck datetime,TimeIO datetime,Ranking int)

Insert into @tab2
Select *, Rank() over (Partition by ID_EMP,dateCheck order by TimeIO) as Ranking
from @tab


Select t1.ID_EMP,t1.FullName,t1.DateCheck,t1.TimeIO as TimeIn ,t2.TimeIO as TimeOut 
from @tab2 t1
Join @tab2 t2 on t1.ID_EMP=t2.ID_EMP and t1.DateCheck=t2.DateCheck and t1.Ranking=t2.Ranking -1
where (t1.Ranking  & 1) = 1