【发布时间】:2017-01-27 11:00:06
【问题描述】:
我有如下表格
id timestamp speed
1 11:00:01 100
2 11:05:01 110
3 11:10:01 90
4 11:15 :01 80
我需要像下面这样计算移动平均线
id timestamp speed average
1 11:00:01 100 100
2 11:05:01 110 105
3 11:10:01 90 100
4 11:15:01 80 95
我尝试了什么
SELECT
*,
(select avg(speed) from tbl t where tbl.timestamp<=t.timestamp) as avg
FROM
tbl
起初看起来很简单,但是当表上的数据膨胀时,它就太慢了
有更快的方法吗?
【问题讨论】:
-
时间增量是否始终不变?否则使用
avg是不正确的。