MySQL:员工生产时数计算

MySQL:员工生产时数计算,mysql,report,Mysql,Report,我需要使用MySQL计算员工的工作时间。 其中,生物测量将捕获相应员工的上班时间和下班时间 emp_设备_考勤表中的数据如下所示: **log_date device_id emp_code device_direction** 2017-09-25 19:34:14 108 400148 out 2017-09-25 14:07:13 106 400148 in 2017-09-25 13:25

我需要使用MySQL计算员工的工作时间。 其中,生物测量将捕获相应员工的上班时间和下班时间

emp_设备_考勤表中的数据如下所示:

**log_date          device_id   emp_code    device_direction**
2017-09-25 19:34:14  108         400148          out
2017-09-25 14:07:13  106         400148          in
2017-09-25 13:25:10  108         400148          out
2017-09-25 10:45:03  106         400148          in
emp_id  date         in_time    out_time    total_hrs   productive_hrs
400148  2017-09-25  10:45:03    19:34:14    08:49:11    08:07:41
从上表中,我需要通过排除他花费的时间来表示一份报告。意味着我们需要展示他在办公室的时间

输出应如下所示:

**log_date          device_id   emp_code    device_direction**
2017-09-25 19:34:14  108         400148          out
2017-09-25 14:07:13  106         400148          in
2017-09-25 13:25:10  108         400148          out
2017-09-25 10:45:03  106         400148          in
emp_id  date         in_time    out_time    total_hrs   productive_hrs
400148  2017-09-25  10:45:03    19:34:14    08:49:11    08:07:41
有人能在MySQL中帮我做这个吗。

让我解释一下这个查询

子查询在\u计时:

此子查询用于计算用户在特定日期的所有时间。生成一个行号,其 如果emp_代码或日期日志_日期发生变化,则重置值

子查询输出时间:

此子查询用于计算用户在特定日期的所有外出时间。生成一个行号,其 如果emp_代码或日期日志_日期发生变化,则重置值

结果: 现在根据emp_代码、datelog_日期和row_编号连接这两个子查询

现在使用timediff和sec_to_time和time_to_sec,您可以获得所需的结果

我希望这对你有帮助

create table  emp_device_attendance (
  log_date timestamp,
  device_id int,
  emp_code varchar(20),
  device_direction varchar(5)
);

insert into emp_device_attendance values('2017-09-25 19:34:14',108,'400148','out');
insert into emp_device_attendance values('2017-09-25 14:07:13',106,'400148','in');
insert into emp_device_attendance values('2017-09-25 13:25:10',108,'400148','out');
insert into emp_device_attendance values('2017-09-25 10:45:03',106,'400148','in');

set @inNumber = 0; 
set @inEmpCode = '';
set @inLogDate = '';
set @outNumber = 0; 
set @outEmpCode = '';
set @outLogDate = '';

select abc.emp_code,abc.date1,min(in_time) ,max(out_time), 
timediff(max(out_time), min(in_time) ) total_hrs,
sec_to_time(sum(productive_hrs)) productive_hrs
from ( 
select in_timing.emp_code,in_timing.date1, 
min(in_timing.log_date)  as in_time, 
max(out_timing.log_date)  as out_time,
time_to_sec(timediff(max(out_timing.log_date), min(in_timing.log_date) )) productive_hrs
from 
( select device_id,emp_code,device_direction, date(log_date) as date1, log_date ,
  if(@inEmpCode = emp_code and @inLogDate = date(log_date) , @inNumber := @inNumber + 1 ,@inNumber := 1) row_number,
  @inEmpCode := emp_code ,@inLogDate := date(log_date)

  from emp_device_attendance
  where  
    device_direction = 'in'
    order by log_date, emp_code
) in_timing join 
(select device_id,emp_code,device_direction, date(log_date) as date1, log_date ,
  if(@outEmpCode = emp_code and @outLogDate = date(log_date) , @outNumber := @outNumber + 1 ,@outNumber := 1) row_number,
  @outEmpCode := emp_code ,@outLogDate := date(log_date)

  from emp_device_attendance
  where  
    device_direction = 'out'
    order by log_date, emp_code ) out_timing on in_timing.row_number = out_timing.row_number
and in_timing.emp_code = out_timing.emp_code
and in_timing.date1 = out_timing.date1

group by in_timing.emp_code,in_timing.date1
,in_timing.row_number ) abc
group by abc.emp_code,abc.date1

这里有一个想法。我应该说,这对您的数据集做出了某些假设,这可能是真的,也可能不是真的,但它应该让您了解

SELECT emp_code
     , DATE(log_date) dt
     , SEC_TO_TIME(SUM(subtotal)) t 
  FROM
     ( SELECT x.emp_code
            , x.log_date
            ,TIME_TO_SEC(x.log_date)-TIME_TO_SEC(MAX(y.log_date)) subtotal 
         FROM emp_device_attendance x 
         JOIN emp_device_attendance y 
           ON y.emp_code = x.emp_code 
          AND y.log_date < x.log_date 
          AND y.device_direction = 'in' 
        WHERE x.device_direction = 'out' 
        GROUP 
           BY x.emp_code,x.log_date
     ) a
 GROUP 
   BY emp_code,dt;

一名员工在任何一天只输入两次?他可以有多个输入和输出的条目,每个输入和输出都有保存的记录,但示例并不能充分反映现实。天哪,这是一个很大的疑问:-@草莓我想是的,但是它处理所有的测试用例,因为它有那么多的查询。@FahadAnjum查询返回的总小时数和生产小时数是相同的,但情况并非如此。当我们在同一天有多个输入和输出时,生产时间会有所不同。请您将测试数据添加一次好吗?这对我来说是不一样的。你测试过两次以上吗?@FahadAnjum我不需要;--但它确实假设每个“入”都有一天“出”