Using the data below I am trying to calculate the difference between timein1 and timeout1, calculate difference between timein2 and timeout2, add the differences together to get the total hh:mm:ss per row and then calculate combined total hh:mm:ss for the entire query. I have managed to retrieve everything but the total time.
** SAMPLE DATA *******************************
CREATE TABLE tblemptimelog
(
recid
INTEGER,
empid
INTEGER,
workdate
DATE,
timein1
TIME,
timeout1
TIME,
timein2
TIME,
timeout2
TIME,
override
VARCHAR(255)
) ENGINE=myisam DEFAULT CHARSET=utf8;
SET autocommit=1;
INSERT INTO tblemptimelog
() VALUES (1, 1832, '2020-04-13', '08:00:00', '12:00:00', '13:10:05', '17:00:00', NULL);
INSERT INTO tblemptimelog
() VALUES (2, 1832, '2020-04-14', '08:00:00', '12:00:00', '13:00:00', '21:00:00', NULL);
INSERT INTO tblemptimelog
() VALUES (3, 1832, '2020-04-15', '08:00:00', '12:00:00', '13:00:00', NULL, NULL);
INSERT INTO tblemptimelog
() VALUES (4, 1832, '2020-04-16', '08:00:00', '12:00:00', '13:00:00', '17:00:00', NULL);
INSERT INTO tblemptimelog
() VALUES (5, 1832, '2020-04-17', '08:00:00', NULL, NULL, NULL, NULL);
INSERT INTO tblemptimelog
() VALUES (6, 1832, '2020-04-18', '08:00:00', '12:00:00', '13:00:00', NULL, NULL);
INSERT INTO tblemptimelog
() VALUES (7, 1831, '2020-04-19', '08:00:00', '12:00:00', '13:00:00', '23:00:00', NULL);
Here is the query I have so far:
SELECT *,
ADDTIME(TIMEDIFF(timeout1, timein1), TIMEDIFF(timeout2, timein2)) diff_time_1_and_2
FROM tblemptimelog
WHERE empid = 1832
ORDER BY workdate
The results are put into a table on a web page and displayed as:
|Emp ID | Date | Time In | Time Out | Time In | Time Out | Total Time
|1831 | 2020-04-13 | 08:00:00 | 12:00:00 | 13:10:05 | 17:00:00 | 07:49:55
|1831 | 2020-04-14 | 08:00:00 | 12:00:00 | 13:00:00 | 21:00:00 | 12:00:00
|1831 | 2020-04-15 | 08:00:00 | 12:00:00 | 13:00:00 | |
|1831 | 2020-04-16 | 08:00:00 | 12:00:00 | 13:00:00 | 17:00:00 | 08:00:00
|1831 | 2020-04-17 | 08:00:00 | | | |
|1111 | 2020-04-18 | 08:00:00 | 12:00:00 | 13:00:00 | |
This is working as it should. I am running into problems totaling the diff_time_1_and_2 column which should total 27:49:55, which will be displayed at the end of the table as shown below:
|Emp ID | Date | Time In | Time Out | Time In | Time Out | Total Time
|1831 | 2020-04-13 | 08:00:00 | 12:00:00 | 13:10:05 | 17:00:00 | 07:49:55
|1831 | 2020-04-14 | 08:00:00 | 12:00:00 | 13:00:00 | 21:00:00 | 12:00:00
|1831 | 2020-04-15 | 08:00:00 | 12:00:00 | 13:00:00 | |
|1831 | 2020-04-16 | 08:00:00 | 12:00:00 | 13:00:00 | 17:00:00 | 08:00:00
|1831 | 2020-04-17 | 08:00:00 | | | |
|1111 | 2020-04-18 | 08:00:00 | 12:00:00 | 13:00:00 | |
**TOTAL TIME: 27:49:55**
I tried the following query:
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(diff_time_1_and_2))) total_time from ( SELECT ADDTIME( TIMEDIFF(timeout1, timein1), TIMEDIFF(timeout2, timein2)) diff_time_1_and_2 FROM tblemptimelog WHERE empid = 1832 ORDER BY workdate )
and got an error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LIMIT 0, 25' at line 1
I also tried the following query:
SELECT *, ADDTIME(TIMEDIFF(timeout1, timein1), TIMEDIFF(timeout2, timein2)) diff_time_1_and_2, SEC_TO_TIME(SUM(TIME_TO_SEC(diff_time_1_and_2))) total_time FROM tblemptimelog WHERE empid = 1832 ORDER BY workdate
and I got the following error:
#1054 - Unknown column 'diff_time_1_and_2' in 'field list'
I have tried variations of the last 2 queries, but I still get one of the two errors above.
I hope I included enough information for someone to give me assistance.