I have a SQL problem that, I think, go far beyond my current knowledge. I have two tables and in each 2 dates in timestamp. I would like to have the average time between those 2 dates, average involving all fields entered in the tables.
I hope I would be clearer doing a simplification of my SQL tables and what I want to do :
-- Table 1 : study
-- -- id
-- -- timestamp (date of input, start of the study for instance)
-- Table 2 : study_monitoring
-- -- id
-- -- id_study
-- -- timestamp_2 (start of the first stage, for instance)
I want to get the average time between timestamp
and timestamp_2
.
I have first to link the study_monitoring
field to its proper study field thanks to the ID located in table 2.
Then to do the difference between the 2 timestamps (the second being always superior to the other). Repeating this for all the fields and finally get the average.
Doing it in several SQL requests wouldn't bother me, it would be in a simple PHP function that return the average of the difference of timestamp.
I don't really know where to start, does the modelling of my tables allow such computing ?
Thanks in advance !