This are the logs of employee # 16
And here is my expected output
我尝试过这个查询:
SELECT a.`id` AS employ_id,
ADDTIME(MIN(a.adjustedDateTime), '12:00:00') AS check_in_time,
ADDTIME(MAX(a.adjustedDateTime), '12:00:00') AS check_out_time,
TIMEDIFF(
MAX(a.adjustedDateTime),
MIN(a.adjustedDateTime) ) AS working_time,
COUNT(0) AS report_times,
DATE_FORMAT(a.adjustedDateTime, '%Y-%m-%d') AS report_date
FROM (SELECT
`EnNo` AS id,
SUBTIME(
CONCAT(DATE(DateTime), ' ', TIME(DateTime)), '12:00:00') AS adjustedDateTime
FROM
bio) a
GROUP BY a.`id`,
DATE_FORMAT(a.adjustedDateTime, '%Y-%m-%d')
ORDER BY a.`id`, DATE('DateTime');
[这是输出,不计算白类时间:
--- Empid - | ----------TimeIn---------- |----------TimeOut------- |----------Hours----- |
00000006 2017-05-15 18:14:13 2017-05-16 06:30:05 12:15:52.000000 00000006 2017-05-16 18:10:18 2017-05-17 05:30:50 11:20:32.000000 00000006 2017-05-18 08:30:05 2017-05-18 08:30:05 00:00:00.000000 00000006 2017-05-18 15:30:05 2017-05-18 15:30:05 00:00:00.000000
有人可以帮我解决这个问题吗?或对其算法或其他任何内容的任何建议。谢谢。抱歉,如果我无法发布图片,因为声誉不够:(
最佳答案
基本上你有以下内容:
- 一条 SQL 语句,给出任何可能是夜类的类次的开始和结束时间,
- 计算时差。
现在,什么是夜类?
- 开始日晚上 10 点之后的所有时间
- 结束日上午 7 点之前的所有时间(我猜测)
因此,您不要简单地使用“工作开始”时间戳,而是使用工作开始到晚上 10 点之间的最大值以及工作结束到上午 7 点之间的最小值:
请注意,对于此夜类计算,我必须添加一些条件来猜测哪个是可能的工作结束时间(中午之前)以及哪个是可能的开始工作时间(中午之后):
create table worktime_stackoverflow (id int, DateTime datetime);
delete from worktime_stackoverflow where id > 0;
insert into worktime_stackoverflow values (1, '2017-05-15 18:14:13');
insert into worktime_stackoverflow values (2, '2017-05-16 06:30:05');
insert into worktime_stackoverflow values (3, '2017-05-17 22:30:45');
insert into worktime_stackoverflow values (4, '2017-05-18 09:30:05');
insert into worktime_stackoverflow values (5, '2017-05-20 10:45:15');
insert into worktime_stackoverflow values (6, '2017-05-20 19:31:25');
insert into worktime_stackoverflow values (5, '2017-05-21 16:45:15');
insert into worktime_stackoverflow values (6, '2017-05-22 03:17:25');
select DATE(w.DateTime) day_of_report
# Start of nightshift
, CONCAT(DATE(MIN(w.DateTime)), ' ' , '22:00:00') start_nightshift,
# actual start of work
MIN(w.DateTime) start_of_work,
# mathematical max() of the two
GREATEST(
CONCAT(DATE(MIN(w.DateTime)), ' ' , '22:00:00'),
Min(w.DateTime)
) nightshift_work_start,
# end of nightshift-hours
CONCAT(ADDDATE(DATE(MIN(w.DateTime)), INTERVAL 1 DAY), ' ' , '07:00:00') end_nightshift
# the following worklog entry (= end of work)
,(SELECT MIN(w2.DateTime) as following_time FROM worktime_stackoverflow w2 WHERE w2.DateTime > w.DateTime AND TIME(w2.DateTime) < '12:00:00'
AND DATEDIFF(w2.DateTime, w.DateTime) < 2) as end_of_work
# mathematical minimum of these two
,LEAST(
(SELECT MIN(w2.DateTime) as following_time FROM worktime_stackoverflow w2 WHERE w2.DateTime > w.DateTime AND TIME(w2.DateTime) < '12:00:00'
AND DATEDIFF(w2.DateTime, w.DateTime) < 2),
CONCAT(ADDDATE(DATE(MIN(w.DateTime)), INTERVAL 1 DAY), ' ' , '07:00:00')
) nightshift_work_end
from worktime_stackoverflow w
# make sure to not use end-of-nightshift loutout times
where TIME(w.DateTime) > '12:00:00'
GROUP by DATE(w.DateTime);
对于最终解决方案,您必须添加员工 ID 等...
关于java - 使用多个日志计算/确定夜类时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44243610/