running average in mysql

后端 未结 4 1278
刺人心
刺人心 2021-01-25 06:15

I have the table like below

id   timestamp  speed
1    11:00:01   100
2    11:05:01   110
3    11:10:01   90
4    11:15 :01  80

I need to calcu

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-25 07:07

    Does MySQL support windowing functions?

    select
      id, timestamp, speed,
      avg (speed) over (order by timestamp) as average
    from tbl
    

    If it doesn't this might work, although I doubt it's efficient:

    select
      min (t1.id) as id, t1.timestamp, min (t1.speed) as speed,
      avg (t2.speed)
    from
      tbl t1
      join tbl t2 on
        t2.id <= t1.id
    group by
      t1.timestamp
    order by
      t1.timestamp
    

提交回复
热议问题