Avg Time difference in mysql

后端 未结 4 1313
被撕碎了的回忆
被撕碎了的回忆 2021-02-12 19:08

Is there a function to find average time difference in the standard time format in my sql.

4条回答
  •  误落风尘
    2021-02-12 19:23

    You can use timestampdiff to find the difference between two times.

    I'm not sure what you mean by "average," though. Average across the table? Average across a row?

    If it's the table or a subset of rows:

    select
        avg(timestampdiff(SECOND, startTimestamp, endTimestamp)) as avgdiff
    from
        table
    

    The avg function works like any other aggregate function, and will respond to group by. For example:

    select
        col1,
        avg(timestampdiff(SECOND, startTimestamp, endTimestamp)) as avgdiff
    from
        table
    group by col1
    

    That will give you the average differences for each distinct value of col1.

    Hopefully this gets you pointed in the right direction!

提交回复
热议问题