简体   繁体   中英

How to calculate work hours per project per employee

I have a PHP form that inputs empID, projectNumber, and clock-in/clock-out time-stamp into a MySQL table like this:

Having no reputation, I can't post image, so take a look here: screenshot http://mailed.in/timecard/ss1.jpg

I need help in generating a report that looks like this: screenshot http://mailed.in/timecard/ss2.jpg

Can I do this entirely in MySQL? How?

This may help you :

SELECT 
    empID AS EmpID, 
    projectNumber AS ProjectNumber, 
    DATE(clocktime) AS StartDate,  
    TIMEDIFF(
      (SELECT max(clocktime) from tableName where DATE( `clocktime` ) = CURDATE( )), 
      (SELECT min(clocktime) from tableName where DATE( `clocktime` ) = CURDATE( ))
    ) AS WorkHours
FROM `tableName`
WHERE 
DATE( `clocktime` ) = CURDATE( )
GROUP BY empID

Try this query -

CREATE TABLE table_proj (
  empid INT(11) DEFAULT NULL,
  projectnumber INT(11) DEFAULT NULL,
  clocktime DATETIME DEFAULT NULL
);

INSERT INTO table_proj VALUES 
  (1, 1, '2011-09-27 10:02:22'),
  (1, 1, '2011-09-27 11:17:32'),
  (2, 2, '2011-09-27 11:34:13'),
  (3, 3, '2011-09-27 11:01:21'),
  (3, 3, '2011-09-27 13:36:28'),
  (2, 2, '2011-09-27 13:55:39'),
  (4, 4, '2011-09-27 14:25:07');

SELECT
  empid, projectnumber, MIN(clocktime) startdate, TIMEDIFF(MAX(clocktime), MIN(clocktime)) workhours
FROM
  table_proj
GROUP BY
  empid, projectnumber
HAVING
  COUNT(*) = 2;

+-------+---------------+---------------------+-----------+
| empid | projectnumber | startdate           | workhours |
+-------+---------------+---------------------+-----------+
|     1 |             1 | 2011-09-27 10:02:22 | 01:15:10  |
|     2 |             2 | 2011-09-27 11:34:13 | 02:21:26  |
|     3 |             3 | 2011-09-27 11:01:21 | 02:35:07  |
+-------+---------------+---------------------+-----------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM