这是我的桌子:
CREATE TABLE IF NOT EXISTS Data(
Time TIMESTAMP(3),
Ms1 FLOAT,
Ms1roll FLOAT)
计算滚动平均值之前的数据示例为:
Time | Ms1 | Ms1roll | +-------------------------+----------+--------- | 2019-11-16 21:26:56.248 | 22.4398 | NULL | | 2019-11-16 21:27:08.251 | 22.4513 | NULL | | 2019-11-16 21:27:20.231 | 22.4515 | NULL | | 2019-11-16 21:27:32.245 | 22.4513 | NULL | | 2019-11-16 21:27:44.245 | 22.4515 | NULL | | 2019-11-16 21:27:56.251 | 22.4514 | NULL | ....
我想计算时间与时间之间的滚动平均值-6分钟。 我尝试过:
A:
update Data set Ms1roll=select b.avg from (select temp.avg,temp.Time FROM (select Data.Time, Data.Ms2, avg(Data_past.Ms1) as avg from Data join Data as Data_past on Data_past.Time between Data.Time - INTERVAL 6 MINUTE and Data.Time group by 1, 2) as temp) as b where b.Time=Data.Time;
B:
update Data as t1
INNER JOIN (select temp.avg,temp.Time FROM (select Data.Time, Data.Ms2, avg(Data_past.Ms1) as avg from Data join Data as Data_past on Data_past.Time between Data.Time - INTERVAL 6 MINUTE and Data.Time group by 1, 2) as temp) as t2 on t2.Time=t1.Time set t1.Ms1Roll=t2.avg
我得到:
+-------------------------+----------+---------+ | Time | Ms1 | Ms1roll | +-------------------------+----------+---------+ | 2019-11-16 21:32:43.633 | 22.4398 | 22.4398 | | 2019-11-16 21:32:43.633 | 22.4513 | 22.4455 | | 2019-11-16 21:32:43.633 | 22.4515 | 22.4475 | | 2019-11-16 21:32:43.633 | 22.4513 | 22.4485 | | 2019-11-16 21:32:43.633 | 22.4515 | 22.4491 | | 2019-11-16 21:32:43.633 | 22.4514 | 22.4495 | ...
为什么时间戳会更改?
感谢您的帮助, 查理。