I have a table like this: SQLfiddle: http://www.sqlfiddle.com/#!9/340c8
CREATE TABLE Table1
(ID int, empid int, time datetime, state int);
+--------------+---------------------+-----------------+
| empid | time | state |
+--------------+---------------------+-----------------+
| 1 | 2011-08-16 14:59:08 | 0 |
| 1 | 2011-08-16 15:00:06 | 1 |
| 1 | 2011-08-16 15:14:51 | 0 |
| 2 | 2011-08-16 15:15:00 | 0 |
| 1 | 2011-08-16 15:59:01 | 1 |
| 2 | 2011-08-16 15:59:09 | 1 |
+--------------+---------------------+-----------------+
Where 0 means login and 1 logout.
I am trying to get the total hours worked per employee per day:
1 2011-08-16 total hours worked 08:32:00
With my query:
SELECT CONCAT(
MOD(TIMEDIFF(MAX(CASE WHEN state = '1' THEN time END),
MIN(CASE WHEN state = '0' THEN time END)), 24 ), ' hours ',
MINUTE(TIMEDIFF(MAX(CASE WHEN state = '1' THEN time END),
MIN(CASE WHEN state = '0' THEN time END))), ' minutes') as HoursWorked,
empid,
Date(time)
FROM emplog T
GROUP BY empid, Date(time)
I get the time worked per day, but only for the min max in out times so I am missing all employees that come in and out several times per day. Any ideas?