您好我从生物测量设备获取数据,如: -
|Id |EmpCode | WorkDate |InOutMode
|247 |51 | 2017-02-13 20:08:52.000 |0
|392 |51 | 2017-02-13 22:38:51.000 |1
|405 |51 | 2017-02-13 22:59:18.000 |0
|415 |51 | 2017-02-13 23:18:17.000 |1
|423 |51 | 2017-02-13 23:33:44.000 |0
|456 |51 | 2017-02-13 01:30:15.000 |1
|463 |51 | 2017-02-13 02:52:02.000 |0
|483 |51 | 2017-02-13 05:11:54.000 |1
|1034 |51 | 2017-02-14 20:09:23.000 |0
|1172 |51 | 2017-02-14 21:59:23.000 |1
|1217 |51 | 2017-02-14 22:30:28.000 |0
|1214 |51 | 2017-02-14 22:30:39.000 |0
|1238 |51 | 2017-02-14 22:49:51.000 |1
|1257 |51 | 2017-02-14 23:19:10.000 |0
|1315 |51 | 2017-02-14 05:04:16.000 |1
|1323 |51 | 2017-02-14 05:05:17.000 |0
|1329 |51 | 2017-02-14 05:08:17.000 |1
|1330 |51 | 2017-02-14 05:08:18.000 |1
我想从上面的表记录中获取数据,如: -
|EmpCode |WorkDate |CheckIn |CheckOut |TotalHours
|51 |2017-02-13 |20:08:52 |22:38:51 |2.499722000
|51 |2017-02-13 |22:59:18 |23:18:17 |0.316388000
|51 |2017-02-13 |23:33:44 |01:30:15 |3.103330000
|51 |2017-02-13 |02:52:02 |05:11:54 |2.331111000
|51 |2017-02-14 |20:09:23 |21:59:23 |1.833333000
|51 |2017-02-14 |22:30:28 |22:49:51 |0.323055000
|51 |2017-02-14 |23:19:10 |05:04:16 |5.323055000
|51 |2017-02-14 |05:05:17 |05:08:18 |0.050000000
PS:重复的IN或OUT被忽略原始数据中的第13,14,17和18行。 2.分钟数小时是小时计算中的小时数。
我需要有关Sql-Server查询的帮助才能获得这些结果。
我当前的代码对我没有帮助,也留下一些行并得到错误的结果和总共几个小时谢谢:)
注意: - 当我的查询执行时缺少两行: -
|456 |51 | 2017-02-13 01:30:15.000 |1
|463 |51 | 2017-02-13 02:52:02.000 |0
答案 0 :(得分:3)
假设In中的0
和1
已出局。
我添加了Overnight
列,以便在第二天1
时返回CheckOut
。如果您不需要,可以将其评论出来。
使用cross apply()
rextester:http://rextester.com/ENFRC28977
with cte as (
select
Id
, EmpCode
, WorkDate
, InOutMode
, Lag_InOutMode = Lag(InOutMode) over (order by EmpCode, WorkDate)
from t
)
select
i.EmpCode
, WorkDate = convert(varchar(10),convert(date,i.WorkDate))
, Overnight = case when datediff(day,i.WorkDate,o.WorkDate)>0 then 1 else 0 end
, CheckIn = convert(time,i.WorkDate)
, CheckOut = convert(time,o.WorkDate)
, TotalHours = datediff(second,i.WorkDate,o.WorkDate)/3600.0
from cte i
cross apply (
select top 1 WorkDate
from cte o
where o.EmpCode = i.EmpCode
and o.InOutMode = 1
and o.Lag_InOutMode != 1
and o.WorkDate > i.WorkDate
order by o.WorkDate asc
) as o
where i.InOutMode = 0
and i.Lag_InOutMode != 0
order by i.WorkDate
返回:
+---------+------------+-----------+----------+----------+------------+
| EmpCode | WorkDate | Overnight | CheckIn | CheckOut | TotalHours |
+---------+------------+-----------+----------+----------+------------+
| 51 | 2017-02-13 | 0 | 02:52:02 | 05:11:54 | 2,331111 |
| 51 | 2017-02-13 | 0 | 20:08:52 | 22:38:51 | 2,499722 |
| 51 | 2017-02-13 | 0 | 22:59:18 | 23:18:17 | 0,316388 |
| 51 | 2017-02-13 | 1 | 23:33:44 | 05:04:16 | 5,508888 |
| 51 | 2017-02-14 | 0 | 05:05:17 | 05:08:17 | 0,050000 |
| 51 | 2017-02-14 | 0 | 20:09:23 | 21:59:23 | 1,833333 |
| 51 | 2017-02-14 | 0 | 22:30:28 | 22:49:51 | 0,323055 |
+---------+------------+-----------+----------+----------+------------+
我在0
之前没有看到InOutMode
'2017-02-13 01:30:15'
,因此我的结果不包含以下行:
|51 |2017-02-13 |23:33:44 |01:30:15 |3.103330000