开发者

How to calculate work hours per project per employee

开发者 https://www.devze.com 2023-04-07 14:48 出处:网络
I have a PHP form that inputs empID, projectNumber, and clock-in/clock-out time-stamp into a MySQL table like this:

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开发者_Go百科 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  |
+-------+---------------+---------------------+-----------+
0

精彩评论

暂无评论...
验证码 换一张
取 消