如何使用mysql查询计算员工迟到和早到的时间

时间:2015-01-29 05:26:27

标签: mysql

当table is devicedetails包括intime,outtime,empcode,status。使用这些列,我想要计算迟到和早期...如果min(intime> 09:30:00)是late.if max(outtime< 18:30:00)是早起..mysql>

select * from devicedetails;

+-----+---------+----------+----------+------------+----------+--------+
| sno | empcode | intime   | outtime  | punchdate  | Lunch    | status |
+-----+---------+----------+----------+------------+----------+--------+
|   1 | 1001    | 09:00:00 | 18:00:00 | 2015-01-23 | 12:30:00 | P      |
|   3 | 1002    | 09:20:00 | 18:20:00 | 2015-01-23 | 12:30:00 | P      |
|   4 | 139634  | 09:00:00 | 19:00:00 | 2015-01-24 | 12:30:00 | P      |
|   5 | 160173  | 09:30:00 | 18:30:00 | 2015-01-10 | 12:30:00 | P      |
|   6 | 160173  | 11:30:00 | 17:30:00 | 2015-01-10 | 12:30:00 | P      |
|   7 | 160173  | 12:30:00 | 19:30:00 | 2015-01-10 | 12:30:00 | P      |
|   8 | 160178  | 10:00:00 | 17:00:00 | 2015-01-11 | 12:30:00 | P      |
|   9 | 160189  | 09:30:02 | 18:29:02 | 2015-01-12 | 12:30:00 | P      |
|  10 | 160198  | 07:30:02 | 17:29:02 | 2015-01-16 | 12:30:00 | P      |
|  11 | 160216  | 12:30:02 | 20:29:02 | 2015-01-17 | 12:30:00 | P      |
|  12 | 160216  | 00:00:00 | 21:29:02 | 2015-01-17 | 12:30:00 | P      |
|  13 | 160489  | 06:00:00 | 16:00:00 | 2015-01-17 | 12:30:00 | P      |
|  14 | 160575  | 09:30:00 | 18:30:00 | 2015-01-18 | 12:30:00 | P      |
|  15 | 160577  | 09:31:00 | 18:31:00 | 2015-01-18 | 12:30:00 | P      |
|  16 | 160598  | 09:29:00 | 18:29:00 | 2015-01-19 | 12:30:00 | P      |
|  17 | 161061  | 08:29:00 | 17:29:00 | 2015-01-20 | 12:30:00 | P      |
|  18 | 1610612 | 07:29:00 | 14:29:00 | 2015-01-21 | 12:30:00 | P      |
|  19 | 175064  | 07:45:00 | 15:35:05 | 2015-01-23 | 12:30:00 | P      |
|  20 | 176261  | 07:45:38 | 15:35:59 | 2015-01-24 | 12:30:00 | P      |
|  21 | 176374  | 06:56:59 | 17:35:45 | 2015-01-25 | 12:30:00 | P      |
|  22 | 176374  | 00:00:00 | 12:35:45 | 2015-01-25 | 12:30:00 | P      |
|  23 | 176374  | 13:00:00 | 00:00:00 | 2015-01-25 | 12:30:00 | P      |
|  24 | 176374  | 00:00:00 | 19:00:00 | 2015-01-25 | 12:30:00 | P      |
+-----+---------+----------+----------+------------+----------+--------+
23 rows in set (0.53 sec)

我使用下面的查询来计算..但我得到的错误是负值和错误的值..

select timediff(max(lasttimeout),min(first_timein)) tottime ,subtime((timediff(max(lasttimeout),min(first_timein))),'08:00:00') otime,ifnull(time_format(time_to_sec(subtime(min(first_timein),'09:30:00')),'%H:%i:%s'),'00:00:00') late,ifnull(time_format(time_to_sec(subtime('18:30:00',max(lasttimeout))),'%H:%i:%s'),'00:00:00') early from devicedetails where empcode='16715';

Plz帮助我......

2 个答案:

答案 0 :(得分:0)

这有帮助吗?

SELECT *,GREATEST(TIMEDIFF('09:30:00',intime),'00:00:00') early FROM devicedetails;

编辑:目前还不清楚你想要什么,但无论如何,以下查询将为每位员工返回每天的加班费......

SELECT empcode
     , punchdate
     , MAX(GREATEST(TIMEDIFF('09:30:00',intime),'00:00:00')) early
     , MAX(GREATEST(TIMEDIFF(outtime,'18:30:00'),'00:00:00')) late
     , ADDTIME(
       MAX(GREATEST(TIMEDIFF('09:30:00',intime),'00:00:00')),
       MAX(GREATEST(TIMEDIFF(outtime,'18:30:00'),'00:00:00')) 
       ) overtime
  FROM my_table
 GROUP
    BY empcode
     , punchdate;

答案 1 :(得分:-1)

@strawberry

从devicedetails开始选择if(min(intime)> '09:30:00',TIMEDIFF(min(intime),'09:30:00'),'00:00:00');