Avg Time difference in mysql

后端 未结 4 2220
清歌不尽
清歌不尽 2021-02-12 19:04

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

4条回答
  •  心在旅途
    2021-02-12 19:27

    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!

提交回复
热议问题