InfluxDB常用函数

InfluxDB常用函数分为4大类,分别是:聚合类,选择类,转换类,预测类。我列举一些常用的。

聚合类

  1. COUNT() 函数:返回一个field中的非空值数量
    例如:
SELECT COUNT(water_level) FROM h2o_feet
SELECT COUNT(water_level) FROM h2o_feet WHERE time >= '2015-08-18T00:00:00Z' AND time < '2015-09-18T17:00:00Z' GROUP BY time(4d)
  1. MEAN() 函数:求平均值,字段类型必须是整型或浮点型
    例如:计算wate_level以4天为间隔的平均值
SELECT MEAN(water_level) FROM h2o_feet WHERE time >= '2015-08-18T00:00:00Z' AND time < '2015-09-18T17:00:00Z' GROUP BY time(4d)
结果
name: h2o_feet
--------------
time               mean
2015-08-17T00:00:00Z     4.322029861111125
2015-08-21T00:00:00Z     4.251395512375667
2015-08-25T00:00:00Z     4.285036458333324
2015-08-29T00:00:00Z     4.469495801899061
2015-09-02T00:00:00Z     4.382785378590083
2015-09-06T00:00:00Z     4.28849666349042
2015-09-10T00:00:00Z     4.658127604166656
2015-09-14T00:00:00Z     4.763504687500006
2015-09-18T00:00:00Z     4.232829850746268
  1. SPREAD()函数:返回字段的最小值和最大值的差值,字段类型必须是整型或浮点型
    例如:计算water_level的最小值 与最大值差,以30分钟间隔,指定location为santa_monica,和一个时间范围
SELECT SPREAD(water_level) FROM h2o_feet WHERE location = 'santa_monica' AND time >= '2015-09-18T17:00:00Z' AND time < '2015-09-18T20:30:00Z' GROUP BY time(30m)
结果
name: h2o_feet
--------------
time                            spread
2015-09-18T17:00:00Z      0.16699999999999982
2015-09-18T17:30:00Z      0.5469999999999997
2015-09-18T18:00:00Z      0.47499999999999964
2015-09-18T18:30:00Z      0.2560000000000002
2015-09-18T19:00:00Z      0.23899999999999988
2015-09-18T19:30:00Z      0.1609999999999996
2015-09-18T20:00:00Z      0.16800000000000015
  1. SUM()函数:求和,字段类型必须是整型或浮点型
    例如:计算以5天为分组,water_level的和
SELECT SUM(water_level) FROM h2o_feet WHERE time >= '2015-08-18T00:00:00Z' AND time < '2015-09-18T17:00:00Z' GROUP BY time(5d)

结果:
--------------
time             sum
2015-08-18T00:00:00Z     10334.908999999983
2015-08-23T00:00:00Z     10113.356999999995
2015-08-28T00:00:00Z     10663.683000000006
2015-09-02T00:00:00Z     10451.321
2015-09-07T00:00:00Z     10871.817999999994
2015-09-12T00:00:00Z     11459.00099999999
2015-09-17T00:00:00Z     3627.762000000003
nearly equivalent 

选择类

  1. TOP()函数:返回字段中最大的前N个值,字段必须是整型或浮点型
    例如:选择3个water_level最大值,并在输出中包含相关的location标记:
SELECT TOP(water_level,3),location FROM h2o_feet
结果
name: h2o_feet
--------------
time               top     location
2015-08-29T07:18:00Z     9.957   coyote_creek
2015-08-29T07:24:00Z     9.964   coyote_creek
2015-08-29T07:30:00Z     9.954   coyote_creek
  1. BOTTOM()函数:返回字段中最小的N个值,使用方法同上

  2. MAX()函数:返回字段中最大的值,该字段类型必须是长整型,float64,或布尔类型。
    例如:每12分选择表h2o_feet中每个location最大的值water_level,指定的时间范围

SELECT MAX(water_level) FROM h2o_feet WHERE time >= '2015-08-18T00:00:00Z' AND time < '2015-08-18T00:54:00Z' GROUP BY time(12m), location
name: h2o_feet
tags: location = coyote_creek
time             max
----             ---
2015-08-18T00:00:00Z      8.12
2015-08-18T00:12:00Z      7.887
2015-08-18T00:24:00Z      7.635
2015-08-18T00:36:00Z      7.372
2015-08-18T00:48:00Z      7.11

name: h2o_feet
tags: location = santa_monica
time             max
----            ---
2015-08-18T00:00:00Z      2.116
2015-08-18T00:12:00Z      2.126
2015-08-18T00:24:00Z      2.051
2015-08-18T00:36:00Z      2.067
2015-08-18T00:48:00Z      1.991
  1. MIN()函数:返回一个字段中的最小值。该字段类型必须是长整型,float64,或布尔类型。
    用法同MAX()

转换类

  1. DIFFERENCE()函数:返回一个字段中连续的时间值之间的差异,字段类型必须是长整型或float64。
    语法:
SELECT DIFFERENCE() FROM  [WHERE ]
与GROUP BY time()以及其他嵌套函数一起使用的语法格式:
SELECT DIFFERENCE(()) FROM  WHERE  GROUP BY time()
其中,函数可以包含以下几个:

COUNT(), MEAN(), MEDIAN(),SUM(), FIRST(), LAST(), MIN(), MAX(), 和 PERCENTILE()。

例如:选择每12分钟间隔最小的water_level值计算这些值之间的差异:

SELECT DIFFERENCE(MIN(water_level)) 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                            difference
2015-08-18T00:12:00Z      -0.03600000000000003
2015-08-18T00:24:00Z      0.0129999999999999
2015-08-18T00:36:00Z      0.026000000000000245
解释:
在不同列中得到值,InfluxDB首先选择min()值每隔12分钟:
> SELECT MIN(water_level) 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             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.028 - 2.064。

预测类

  1. HOLT_WINTERS():返回N个预测字段值
    语法:
SELECT HOLT_WINTERS[_WITH-FIT]((),,) [INTO_clause] FROM_clause [WHERE_clause] GROUP_BY_clause [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause]

更多参考:https://www.cnblogs.com/michellexiaoqi/category/1044214.html

你可能感兴趣的:(InfluxDB常用函数)