4、MOVING_AVERAGE()函数
作用:返回一个连续字段值的移动平均值,字段类型必须是长整形或者float64类型。
例子1:
下面的例子water_level
在表 h2o_feet,
location 为
santa_monica,
时间段
2015 - 08 - 18 - t00:00:00z和2015 - 08 - 18 - t00:36:00z:
> SELECT water_level FROM h2o_feet WHERE location = ‘santa_monica‘ AND time >= ‘2015-08-18T00:00:00Z‘ and time <= ‘2015-08-18T00:36:00Z‘
name: h2o_feet
--------------
time water_level
2015-08-18T00:00:00Z 2.064
2015-08-18T00:06:00Z 2.116
2015-08-18T00:12:00Z 2.028
2015-08-18T00:18:00Z 2.126
2015-08-18T00:24:00Z 2.041
2015-08-18T00:30:00Z 2.051
2015-08-18T00:36:00Z 2.067
计算移动平均在每2字段值:
SELECT MOVING_AVERAGE(water_level,2) FROM h2o_feet WHERE location = ‘santa_monica‘ AND time >= ‘2015-08-18T00:00:00Z‘ and time <= ‘2015-08-18T00:36:00Z‘
结果:
name: h2o_feet
--------------
time moving_average
2015-08-18T00:06:00Z 2.09
2015-08-18T00:12:00Z 2.072
2015-08-18T00:18:00Z 2.077
2015-08-18T00:24:00Z 2.0835
2015-08-18T00:30:00Z 2.0460000000000003
2015-08-18T00:36:00Z 2.059
解释移动平均线列中的第一个值的平均值是2.064和2.116,第二个移动平均线列中的值是2.116和2.028的平均水平。
例子2
- 选择最低值water_level每隔12分钟和计算移动平均每2字段值:
SELECT MOVING_AVERAGE(MIN(water_level),2) FROM h2o_feet WHERE location = ‘santa_monica‘ AND time >= ‘2015-08-18T00:00:00Z‘ and time <= ‘2015-08-18T00:36:00Z‘ GROUP BY time(12m)
结果:
name: h2o_feet
--------------
time moving_average
2015-08-18T00:12:00Z 2.0460000000000003
2015-08-18T00:24:00Z 2.0345000000000004
2015-08-18T00:36:00Z 2.0540000000000003
解释:这些结果,InfluxDB首先选择MIN()
water_level
每12分钟间隔:
name: h2o_feet
--------------
time min
2015-08-18T00:00:00Z 2.064
2015-08-18T00:12:00Z 2.028
2015-08-18T00:24:00Z 2.041
2015-08-18T00:36:00Z 2.067
然后使用这些值来计算移动平均在每2字段值,移动平均线列中的第一个结果的平均值2.064和2.028,第二个结果是2.028和2.041的平均水平。
时间: 2024-11-03 22:43:13