简体   繁体   中英

Grand Total of TIME DIFFERENCE

I want to get the grand total of overtime after endtime and starttime being SUBTRACTED and SUMMED UP by group .

Here's my code:

$sql = "SELECT o.*, u.* ,p.* ,
            SEC_TO_TIME(SUM(TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(dateto, '%m/%d/%y %H:%i'), '%Y-%m-%d %H:%i%s')) - TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(datefrom, '%m/%d/%y %H:%i'), '%Y-%m-%d %H:%i%s')))) AS totalHrs,

            SEC_TO_TIME(SUM(TIME_TO_SEC(totalHrs))) AS grandTotal // Can't make this run
            FROM overtime AS o, users AS u, position AS p
            WHERE o.user_id=u.user_id and u.p_id=p.p_id
            GROUP BY p.p_id
            ORDER BY p.position_name ASC";

Here's my database:

|-----------|-------------|-------------|-----------------------|---------------------|
|-- ot_id --|-- user_id --|--- p_id ----|------ datefrom -------|------- dateto ------|
|-----------|-------------|-------------|-----------------------|---------------------|
|    1      |      3      |      17     |    12/17/15 17:00:00  |   12/17/15 19:00:00 |
|-----------|-------------|-------------|-----------------------|---------------------|
|    2      |      3      |      17     |    12/17/15 17:00:00  |   12/17/15 19:00:00 |
|-----------|-------------|-------------|-----------------------|---------------------|
|    3      |      10     |      27     |   12/17/15 17:00:00   |   12/17/15 19:00:00 |
|-----------|-------------|-------------|-----------------------|---------------------|
|    4      |      45     |      27     |    12/17/15 17:00:00  |   12/17/15 19:00:00 |
|-----------|-------------|-------------|-----------------------|---------------------|
|    5      |      44     |      5      |   12/17/15 17:00:00   |   12/17/15 19:00:00 |
|-----------|-------------|-------------|-----------------------|---------------------|

This is the output:

p_id #17 ----> Junior Programmer
     #27 ----> SAP Programmer
     #5 -----> Technician

|----------------------|--------------|
|------ position ------|-- Total Hrs--|
|----------------------|--------------|
|   Junior Programmer  |    4:00:00   |
|----------------------|--------------|
|    SAP Programmer    |    4:00:00   |
|----------------------|--------------|
|      Technician      |    2:00:00   |
|----------------------|--------------|
|       GRAND TOTAL    |   10:00:00   | <-------- CAN'T GET THIS
|----------------------|--------------|

Any help could do.

Thanks in advance :)

Use WITH ROLLUP function to get cumulative sum of any column.

Try this:

SELECT IFNULL(p.position_name, 'Grand Total') AS `position`
       SEC_TO_TIME(SUM(TIME_TO_SEC(STR_TO_DATE(dateto, '%m/%d/%y %H:%i')) - TIME_TO_SEC(STR_TO_DATE(datefrom, '%m/%d/%y %H:%i')))) AS totalHrs
FROM overtime AS o, users AS u, POSITION AS p
WHERE o.user_id=u.user_id AND u.p_id=p.p_id
GROUP BY p.p_id WITH ROLLUP
ORDER BY p.position_name ASC;

OR

You can also use TIMESTAMPDIFF() function to generate difference to two dates.

SELECT IFNULL(p.position, 'Grand Total') AS `position`
        SEC_TO_TIME(SUM(TIMESTAMPDIFF(SECOND,STR_TO_DATE(datefrom, '%m/%d/%y %H:%i'),STR_TO_DATE(dateto, '%m/%d/%y %H:%i')))) AS totalHrs
FROM overtime AS o, users AS u, POSITION AS p
WHERE o.user_id=u.user_id AND u.p_id=p.p_id
GROUP BY p.p_id WITH ROLLUP
ORDER BY p.position_name ASC

OR

SELECT o.*, u.* ,p.* ,
       SEC_TO_TIME(SUM(TIME_TO_SEC(STR_TO_DATE(dateto, '%m/%d/%y %H:%i')) - TIME_TO_SEC(STR_TO_DATE(datefrom, '%m/%d/%y %H:%i')))) AS totalHrs
FROM overtime AS o, users AS u, POSITION AS p
WHERE o.user_id=u.user_id AND u.p_id=p.p_id
GROUP BY p.p_id WITH ROLLUP
ORDER BY p.position_name ASC

Oh! My flaws, I've found faults in my code.

The %H:%i:%s is necessary in getting the difference and sum of time.

Also I forgot to include : in %H:%i%s .

Adding this line will get the Grand Total:

(select SEC_TO_TIME(SUM(TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(dateto, '%m/%d/%y %H:%i:%s'), '%Y-%m-%d %H:%i:%s')) - TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(datefrom, '%m/%d/%y %H:%i:%s'), '%Y-%m-%d %H:%i:%s')))) FROM overtime WHERE approve_by=0) AS grandTotal

Final Code will be:

SELECT o.*, u.* ,p.* ,
            SEC_TO_TIME(SUM(TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(dateto, '%m/%d/%y %H:%i:%s'), '%Y-%m-%d %H:%i:%s')) - TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(datefrom, '%m/%d/%y %H:%i:%s'), '%Y-%m-%d %H:%i:%s')))) AS totalHrs,

            (select SEC_TO_TIME(SUM(TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(dateto, '%m/%d/%y %H:%i:%s'), '%Y-%m-%d %H:%i:%s')) - TIME_TO_SEC(DATE_FORMAT(STR_TO_DATE(datefrom, '%m/%d/%y %H:%i:%s'), '%Y-%m-%d %H:%i:%s')))) FROM overtime) AS grandTotal

            FROM overtime AS o, users AS u, position AS p
            WHERE o.user_id=u.user_id and u.p_id=p.p_id and o.approve_by=0
            GROUP BY p.p_id
            ORDER BY p.position_name ASC

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