MySQL:员工生产时间计算

时间:2017-10-10 12:40:55

标签: mysql report

我需要使用MySQL计算员工的生产时间。 Bio-Metric将捕获相应员工的IN时间和出时间。

emp_device_attendance 表中的数据完全如下所示:

**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

从上表中我需要通过排除他花费的时间和#34;来代表一份报告。意味着我们需要知道他在办公室的时间。

输出应如下所示:

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中这样做。

2 个答案:

答案 0 :(得分:0)

让我解释一下查询。

子查询in_timing:

此子查询用于计算用户及特定日期的所有时间。生成一个row_number,其中 如果emp_code或date(log_date)发生更改,则重置值。

子查询out_timing:

此子查询用于计算用户和特定日期的所有时间。生成一个row_number,其中 如果emp_code或date(log_date)发生更改,则重置值。

加入结果: 现在基于emp_code,date(log_date)和row_number加入这两个子查询。

现在使用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

答案 1 :(得分:0)

这是一个想法。我应该说这会对你的数据集做出某些假设,这可能是也可能不是,但它应该给你这个想法......

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;