Subtract timestamps from different tables

96 views Asked by At

We have a designed survey that collects timestamps of the time a decision is made (auditquestionid 12345) and the time the action is performed (auditquestionid 12346). A mysql 'survey' table stores information about the timing of the survey, and then the timestamp values are kept in a separate table ('surveyquestiontimedata') along with the 'surveyid'. I would like to find the time difference between timeaction and timedecision for each survey. Any help much appreciated.

Survey
surveyid | surveydate | auditid
-------------------------------
001      | 20/06/2015 |    12  
002      | 21/06/2015 |    12

Surveyquestiontimedata
surveyid | auditquestionid | value
----------------------------------
001      |   12345         | 10:00  
001      |   12346         | 10:25  
002      |   12345         | 15:25  
002      |   12346         | 16:10

Output
surveyid | difference
001      |    00:15
002      |    00:45
1

There are 1 answers

0
Mahesh Agrawal On

What ever you want to do is not looking clear from your post but i think this solution may help you in getting what you want.

The query will be like this

SELECT TIMESTAMPDIFF
(SECOND, s.timeaction, 
(SELECT std.timedecision 
FROM surveytimedata std 
WHERE std.surveyid = s.surveyid 
ORDER BY std.timedecision DESC 
LIMIT 1)) 
FROM survey s

By this you will get the difference in seconds between the timeaction from survey table and the latest timedecision from surveytimedata table.