在SQL中查找中断时间

时间:2015-08-13 20:34:36

标签: sql sql-server-2008

我需要为用户找到休息时间。我有一个具有登录时间,注销时间,原因代码等的表。基本上,ENDSHIFT条目之后的第一次输入是新一天的登录时间。

示例表:时间

==>if exist p q\*.txt echo yes

==>if exist "p q\*.txt" echo yes

==>

我正在寻找一个结果simalar:

FIRST_NAME  LAST_NAME          USER_ID  LOGIN_TIME              LOGOUT_TIME             REASON_CODE
1.John          Doe             10091   2015-08-10 00:24:10.357 2015-08-10 02:34:47.973 ENDSHIFT
2.John          Doe             10091   2015-08-11 17:13:38.957 2015-08-11 19:15:45.437 BREAK   
3.John          Doe             10091   2015-08-11 19:41:25.613 2015-08-11 21:38:26.380 BREAK   
4.John          Doe             10091   2015-08-11 22:20:23.600 2015-08-11 23:59:58.337 BREAK   
5.John          Doe             10091   2015-08-12 00:23:58.767 2015-08-12 01:53:45.067 ENDSHIFT
6.John          Doe             10091   2015-08-12 17:14:06.747 2015-08-12 18:57:05.660 BREAK   
7.John          Doe             10091   2015-08-12 19:26:11.790 2015-08-12 21:32:21.683 BREAK   
8.John          Doe             10091   2015-08-12 22:13:35.030 2015-08-12 23:22:31.413 ENDSHIFT

2 个答案:

答案 0 :(得分:0)

这是你要找的东西吗?实际上没有测试过代码。

Select
    First_Name
    ,Last_Name
    ,User_ID
    ,convert(varchar(10), dateadd(dd, datediff(dd, 0, Login_Time),0), 111) as "Date"
    ,convert(varchar(5), sum(Datediff(mi, Login_Time, Logout_Time)), 114) as "Break Time"
From
    Times
Where
    Reason = 'Break'
Group by
    First_Name
    ,Last_Name
    ,User_ID
    ,dateadd(dd, datediff(dd, 0, Login_Time),0)

答案 1 :(得分:0)

我最终这样做了。不确定它是否最有效但这就是我所拥有的。

SELECT NAME, OPERATOR, STARTDATE, USERLOG_ID, (SHIFT-BREAKS1)BREAKS, ACTUAL_TIME
FROM(

SELECT a.NAME, a.OPERATOR, a.STARTDATE, a.USERLOG_ID, SUM(b.Breaks)BREAKS1, SUM(c.SHIFT)SHIFT, SUM(a.ACTUAL_TIME)ACTUAL_TIME
FROM(
 select 
    w.NAME,
    rtrim(u.FIRST_NAME)+ ' '+ rtrim(u.LAST_NAME) OPERATOR,
    rtrim(convert(varchar(12),min(ul.LOGIN_TIME),101)) STARTDATE,
    ul.USERLOG_ID,
    oi.INOUTLOG_ID,
    rtrim(substring(cast(min(oi.LOGOUT_TIME) as varchar(20)),12,8)) LOGOUT_TIME,
DATEDIFF(Minute, oi.LOGIN_TIME, oi.LOGOUT_TIME)Breaks,
max(e.DESCRIPTION) EXITREASON,
sum(isnull(a.ACTUAL_TIME/60/60,0))  ACTUAL_TIME
from
userlog ul
left outer join assignment a
    on ul.USERLOG_ID = a.USERLOG_ID
left outer join users u 
    on ul.USER_ID = u.USER_ID
left outer join inoutlog oi
    on ul.USERLOG_ID = oi.USERLOG_ID
left outer join jobtype j
    on j.JOBTYPE_ID = u.JOBTYPE_ID 
left outer join warehouse w
    on w.WAREHOUSE_ID = a.WAREHOUSE_ID
left outer join exitreason e
    on oi.EXITREASON_ID = e.EXITREASON_ID    
where 
ul.LOGIN_TIME between @BeginTime and @EndTime

group by
w.NAME,
ul.USERLOG_ID,
u.FIRST_NAME,
u.LAST_NAME,
oi.INOUTLOG_ID,
ul.LOGIN_TIME,
e.LABOR_ID,
e.LOGOUT,
ul.LOGOUT_TIME,
oi.LOGIN_TIME,
oi.LOGOUT_TIME
  )a
LEFT JOIN(
select 
ul.USERLOG_ID,
DATEDIFF(Minute, oi.LOGIN_TIME, oi.LOGOUT_TIME)Breaks,
oi.INOUTLOG_ID
from
userlog ul
left outer join inoutlog oi
    on ul.USERLOG_ID = oi.USERLOG_ID
left outer join exitreason e
    on oi.EXITREASON_ID = e.EXITREASON_ID
where 
ul.LOGIN_TIME between @BeginTime and @EndTime AND e.DESCRIPTION = 'BREAK'

)b ON a.USERLOG_ID = b.USERLOG_ID AND a.INOUTLOG_ID = b.INOUTLOG_ID
LEFT JOIN(
select 
ul.USERLOG_ID,
DATEDIFF(Minute, ul.LOGIN_TIME, ul.LOGOUT_TIME)SHIFT,
oi.INOUTLOG_ID
from
userlog ul
left outer join inoutlog oi
    on ul.USERLOG_ID = oi.USERLOG_ID
left outer join exitreason e
    on oi.EXITREASON_ID = e.EXITREASON_ID
where 
ul.LOGIN_TIME between @BeginTime and @EndTime AND e.DESCRIPTION = 'END OF SHIFT'
)c ON a.USERLOG_ID = c.USERLOG_ID AND a.INOUTLOG_ID = c.INOUTLOG_ID
GROUP BY a.NAME, a.OPERATOR, a.STARTDATE, a.USERLOG_ID
    )z