2017-05-29 64 views
0

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'); 

[这里是输出,不计算dayshift小时:

--- Empid - | ---------- TimeIn ---------- | ---------- TimeOut ------- | --------- - 小时----- |

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 

谁能帮助我?或任何有关它的算法或任何建议。谢谢。很抱歉,如果我不能发布的图像,没有足够的信誉:(

+0

从业务角度来看,“夜班时间”会是什​​么?什么时候开始,什么时候结束?你可以给你的工作日志表定义表格吗? – Jan

+0

我的夜班定义是晚上10点。我只是需要获得帮助,以获得工作时间的空档和夜班。就像上面给出的一样。我需要帮助:3 – Nicoooooo

+0

您对上班时间的计算似乎也是如此。看起来第3行和第4行从08:30到15:30将是一天移位? – Jan

回答

0

基本上你有以下:

  • 一个SQL statemeng给予开始和结束时间的任何变化,可能已经是一个夜班,
  • 时差计算。

现在,什么是夜班?

  • 全部为H晚上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等等......

+0

这会帮助很多。那些日子呢?假设从上午9点到下午5点。 – Nicoooooo

+0

你说你有那些......那些只是那些在同一天有最小和最大值而不相等的,对吧? – Jan

+0

是的,我愿意。但是在我查询的哪一部分我会插入它?我会将我的查询添加到您的天移。(DateTime),MIN(DateTime)FROM bio GROUP BY DATE('DateTime'),EmpNo; – Nicoooooo