MySQL Find Average Timediff of with group By And Exclusion
I have a MySQL table which contains two datetime columns:
CREATE TABLE test (job_id int, dateCol1 DATETIME, datecol2 DATETIME);
It contains a series of data representing the start and end times of jobs.
I need to know what is the Average duration of those jobs in minutes, but
so far can't see how to achieve it.
I have tried various things:
SELECT job_id, AVG(TIMEDIFF(datecol2,datecol1)) FROM test GROUP BY job_id;
SELECT job_id, SEC_TO_TIME(AVG(TIMEDIFF(datecol2,datecol1))) FROM test
GROUP BY job_id;
SELECT job_id,
SEC_TO_TIME(AVG(TIME_TO_SEC(datecol2)-TIME_TO_SEC(datecol1))) FROM test
GROUP BY job_id;
SELECT job_id,
SEC_TO_TIME(AVG(TIME_TO_SEC(FROM_UNIXTIME(UNIX_TIMESTAMP(datecol2)-UNIX_TIMESTAMP(datecol1)))))
FROM test GROUP BY job_id;;
I'm comparing it to a list of all jobs which I printed out and Averaged in
Excel, but so far I am getting different results.
As an addition I will also need to exclude any where the duration is
greater than 1 hour.
I'm sure I am just overcomplicating this, and if someone can show me the
way it would be appreciated, otherwise Ill end up having to print out a
list for each jobid, and averaging them manually in excel.
No comments:
Post a Comment