InfuxDB函数说明

1. 聚合函数

1.1 COUNT()

描述:返回单个字段中非空(non-null)值的数量
语法

SELECT COUNT() FROM  [WHERE ] [GROUP BY ]

例子:

  • 计算字段water_level非空值的数量
> SELECT COUNT("water_level") FROM "h2o_feet"
name: h2o_feet
--------------
time                           count
1970-01-01T00:00:00Z     15258
  • 计算字段water_level非空值的数量,按时间间隔分组
> 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)
name: h2o_feet
--------------
time                           count
2015-08-17T00:00:00Z     1440
2015-08-21T00:00:00Z     1920
2015-08-25T00:00:00Z     1920
2015-08-29T00:00:00Z     1920
2015-09-02T00:00:00Z     1915
2015-09-06T00:00:00Z     1920
2015-09-10T00:00:00Z     1920
2015-09-14T00:00:00Z     1920
2015-09-18T00:00:00Z     335
  • h2o_feet有两个数据字段level descriptionwater_level,可以通过count(*) 统计所有的字段
> SELECT COUNT(*) FROM "h2o_feet"
name: h2o_feet
--------------
time                   count_level description      count_water_level
1970-01-01T00:00:00Z   15258                       15258

1.2 DISTINCT()

说明:返回字段的唯一值(去重)
语法

SELECT DISTINCT() FROM  [WHERE ] [GROUP BY ]

例子:

  • 全表单字段
> SELECT DISTINCT("level description") FROM "h2o_feet"
name: h2o_feet
--------------
time                           distinct
1970-01-01T00:00:00Z     between 6 and 9 feet
1970-01-01T00:00:00Z     below 3 feet
1970-01-01T00:00:00Z     between 3 and 6 feet
1970-01-01T00:00:00Z     at or greater than 9 feet
  • 分组单字段
> SELECT DISTINCT("level description") FROM "h2o_feet" GROUP BY "location"
name: h2o_feet
tags: location=coyote_creek
time                            distinct
----                            --------
1970-01-01T00:00:00Z      between 6 and 9 feet
1970-01-01T00:00:00Z      between 3 and 6 feet
1970-01-01T00:00:00Z      below 3 feet
1970-01-01T00:00:00Z      at or greater than 9 feet

name: h2o_feet
tags: location=santa_monica
time                            distinct
----                            --------
1970-01-01T00:00:00Z      below 3 feet
1970-01-01T00:00:00Z      between 3 and 6 feet
1970-01-01T00:00:00Z      between 6 and 9 feet
  • 多字段
> SELECT DISTINCT(*) FROM "h2o_feet" LIMIT 5
name: h2o_feet
--------------
time                   distinct_level description    distinct_water_level
1970-01-01T00:00:00Z   below 3 feet                  2.064
1970-01-01T00:00:00Z   between 6 and 9 feet          8.12
1970-01-01T00:00:00Z                                 2.116
1970-01-01T00:00:00Z                                 8.005
1970-01-01T00:00:00Z                                 2.028

1.3 MEAN()

说明:算术平均值,字段类型必须为整型(int64)或浮点型(float64)
语法

SELECT MEAN() FROM  [WHERE ] [GROUP BY ]

例子:

  • 时间在2016-08-18T00:00:00Z2016-09-18T17:00:00Z之间,按4天(time(4d))进行分组
> 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
2016-08-17T00:00:00Z     4.322029861111125
2016-08-21T00:00:00Z     4.251395512375667
2016-08-25T00:00:00Z     4.285036458333324
2016-08-29T00:00:00Z     4.469495801899061
2016-09-02T00:00:00Z     4.382785378590083
2016-09-06T00:00:00Z     4.28849666349042
...

1.4 MEDIAN()

说明:返回中位数,字段类型必须为整型(int64)或浮点型(float64),*表示所有字段
语法

SELECT MEDIAN() FROM  [WHERE ] [GROUP BY ]

例子:

> SELECT MEDIAN("water_level") FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time < '2015-08-18T00:36:00Z' GROUP BY "location"
name: h2o_feet
tags: location = coyote_creek
time                           median
----                           ------
2015-08-18T00:00:00Z     7.8245

name: h2o_feet
tags: location = santa_monica
time                           median
----                           ------
2015-08-18T00:00:00Z     2.0575

1.5 MODE()

说明:返回最频繁的(出现频率最高的)值,如果有多个频率相同的值则返回第一个(最早的)
语法

SELECT MODE() FROM  [WHERE ] [GROUP BY ]

例子:

> SELECT MODE("water_level") FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time < '2015-08-18T00:36:00Z' GROUP BY "location"
name: h2o_feet
tags: location = coyote_creek
time                           mode
----                           ------
2015-08-18T00:00:00Z     7

name: h2o_feet
tags: location = santa_monica
time                           mode
----                           ------
2015-08-18T00:00:00Z     2

1.5 SPREAD()

说明:返回字段的最小值和最大值之间的差值。 该字段必须是int64float64类型; *表示表中的所有int64float64字段。

语法

SELECT SPREAD() FROM  [WHERE ] [GROUP BY ]

例子:

> 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.6 SUM()

说明:返回单个字段中所有值的总和。 该字段必须是int64float64类型; *表示表中的所有int64float64字段。

语法

SELECT SUM() FROM  [WHERE ] [GROUP BY ]

例子:

> 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)
name: h2o_feet
--------------
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

1.7 STDDEV()

说明:返回单个字段中的值的标准偏差。 该字段必须是int64float64类型。
语法

SELECT STDDEV() FROM  [WHERE ] [GROUP BY ]

2. 选择器函数

2.1 BOTTOM()

说明:返回单个字段中最小的N个值。 字段类型必须为int64float64。当同步指定tag时,设定tag的记录数是X,如果N>X时只返回X条记录,每个tag一条,否则返回最小的N条数据。
语法

SELECT BOTTOM([,],)[,] FROM  [WHERE ] [GROUP BY ]

2.2 FIRST()

说明:返回单个字段的最旧值(由时间戳确定)。
语法

SELECT FIRST()[,] FROM  [WHERE ] [GROUP BY ]

2.3 LAST()

说明:返回单个字段的最新值(由时间戳确定)。

SELECT LAST()[,] FROM  [WHERE ] [GROUP BY ]

2.4 MAX()

说明:返回单个字段中的最大值。 该字段必须是int64float64boolean
语法

SELECT MAX()[,] FROM  [WHERE ] [GROUP BY ]

2.5 MIN()

说明:返回单个字段中的最小值。 该字段必须是int64float64boolean
语法

SELECT MIN()[,] FROM  [WHERE ] [GROUP BY ]

2.6 PERCENTILE()

说明:返回单个字段的排序值的第N个百分位数值。 该字段必须是int64float64类型。 百分位数N必须是0100之间的整数或浮点数,包括0100。算法找度娘。
语法

SELECT PERCENTILE(, )[,] FROM  [WHERE ] [GROUP BY ]

2.7 SAMPLE()

说明:返回指定字段的N个点的随机抽样。 InfluxDB使用储层采样来生成随机点。 SAMPLE()支持所有字段类型。
语法

SELECT SAMPLE(,) FROM_clause [WHERE_clause] [GROUP_BY_clause]

2.8 TOP()

说明:返回单个字段中最大的N个值。 字段类型必须为int64float64
语法

SELECT TOP([,],)[,] FROM  [WHERE ] [GROUP BY ]

3. 转换函数

3.1 CUMULATIVE_SUM()

说明:返回单个字段的连续字段值的累积和。 字段类型必须为int64float64
基础语法

SELECT CUMULATIVE_SUM() FROM_clause WHERE_clause

高级语法
支持如下嵌套函数类型:COUNT()
, MEAN()
, MEDIAN()
, MODE()
, SUM()
, FIRST()
, LAST()
, MIN()
,MAX()
, and PERCENTILE()
.

SELECT CUMULATIVE_SUM(()) FROM_clause WHERE_clause GROUP BY time()[,]

例子

##原数据
> SELECT "water_level" FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z' AND "location" = 'santa_monica'
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
##转换后数据
> SELECT CUMULATIVE_SUM("water_level") FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z' AND "location" = 'santa_monica'
name: h2o_feet
time                   cumulative_sum
----                   --------------
2015-08-18T00:00:00Z   2.064
2015-08-18T00:06:00Z   4.18
2015-08-18T00:12:00Z   6.208
2015-08-18T00:18:00Z   8.334
2015-08-18T00:24:00Z   10.375
2015-08-18T00:30:00Z   12.426

3.2 DERIVATIVE()

说明:返回系列中单个字段中值的变化率。 InfluxDB计算时间域值之间的差异,并将这些结果转换为单位变化率。 单位参数是可选的,如果未指定,则默认为1秒(1秒)。
基础语法

SELECT DERIVATIVE(, []) FROM  [WHERE ]

单位unit可以有如下值:

  • u or µ微妙(microseconds)
  • ms 毫秒(milliseconds)
  • s 秒(seconds)
  • m 分(minutes)
  • h 时(hours)
  • d 天(days)
  • w 周(weeks)

使用聚合函数语法

SELECT DERIVATIVE(AGGREGATION_FUNCTION(),[]) FROM  WHERE  GROUP BY time()

例子

#原数据,两条数据直接间隔6分钟
> SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' LIMIT 5
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

#不指定单位时,默认为每秒变化率,如第一条记录(`(2.116-2.064)/(6m/1s)`)
> SELECT DERIVATIVE("water_level") FROM "h2o_feet" WHERE "location" = 'santa_monica' LIMIT 5
name: h2o_feet
--------------
time                           derivative
2015-08-18T00:06:00Z     0.00014444444444444457
2015-08-18T00:12:00Z     -0.00024444444444444465
2015-08-18T00:18:00Z     0.0002722222222222218
2015-08-18T00:24:00Z     -0.000236111111111111
2015-08-18T00:30:00Z     2.777777777777842e-05

#每6分钟变化率,如第一条(`(2.116-2.064)/(6m/6m)`)
> SELECT DERIVATIVE("water_level",6m) FROM "h2o_feet" WHERE "location" = 'santa_monica' LIMIT 5
name: h2o_feet
--------------
time                           derivative
2015-08-18T00:06:00Z     0.052000000000000046
2015-08-18T00:12:00Z     -0.08800000000000008
2015-08-18T00:18:00Z     0.09799999999999986
2015-08-18T00:24:00Z     -0.08499999999999996
2015-08-18T00:30:00Z     0.010000000000000231

#每12分钟变化率,如第一条(`(2.116-2.064)/(6m/12m)`)
> SELECT DERIVATIVE("water_level",12m) FROM "h2o_feet" WHERE "location" = 'santa_monica' LIMIT 5
name: h2o_feet
--------------
time                           derivative
2015-08-18T00:06:00Z     0.10400000000000009
2015-08-18T00:12:00Z     -0.17600000000000016
2015-08-18T00:18:00Z     0.19599999999999973
2015-08-18T00:24:00Z     -0.16999999999999993
2015-08-18T00:30:00Z     0.020000000000000462

#每隔12分钟最大值
> SELECT MAX("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                           max
2015-08-18T00:00:00Z     2.116
2015-08-18T00:12:00Z     2.126
2015-08-18T00:24:00Z     2.051
#每隔12分钟变化率,如第一条(`(2.126-2.116)/(12m-12m)`),带group by 语句时unit默认为time函数指定的间隔
> SELECT DERIVATIVE(MAX("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                           derivative
2015-08-18T00:12:00Z     0.009999999999999787
2015-08-18T00:24:00Z     -0.07499999999999973

#每隔18分钟汇总数据
> SELECT SUM("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(18m)
name: h2o_feet
--------------
time                           sum
2015-08-18T00:00:00Z     6.208
2015-08-18T00:18:00Z     6.218

#每隔18分钟汇总数据没6分钟变化率,如第一条(`(6.218-6.208)/(18m/6m)`)
> SELECT DERIVATIVE(SUM("water_level"),6m) FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= '2015-08-18T00:00:00Z' AND time < '2015-08-18T00:36:00Z' GROUP BY time(18m)
name: h2o_feet
--------------
time                           derivative
2015-08-18T00:18:00Z     0.0033333333333332624

3.3 DIFFERENCE()

说明:返回单个字段中连续的时间顺序值之间的差异。 字段类型必须为int64float64
基础语法

SELECT DIFFERENCE() FROM  [WHERE ]

高级语法

SELECT DIFFERENCE(()) FROM  WHERE  GROUP BY time()

支持的内嵌函数包括:COUNT()
, MEAN()
, MEDIAN()
, SUM()
, FIRST()
,LAST()
, MIN()
, MAX()
, and PERCENTILE()

例子

#带处理数据
> 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
#处理后
> SELECT DIFFERENCE("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                            difference
2015-08-18T00:06:00Z      0.052000000000000046
2015-08-18T00:12:00Z      -0.08800000000000008
2015-08-18T00:18:00Z      0.09799999999999986
2015-08-18T00:24:00Z      -0.08499999999999996
2015-08-18T00:30:00Z      0.010000000000000231
2015-08-18T00:36:00Z      0.016000000000000014
#分组统计后数据
> 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
#分组统计处理后数据
> 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

3.4 ELAPSED()

说明:返回单个字段中后续时间戳之间的差异。 持续时间(unit)参数是可选的,如果未指定,则默认为一纳秒。
语法

SELECT ELAPSED(, ) FROM  [WHERE ]

例子

#原数据
> 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
#每两个`water_level`数据记录直接的时间间隔是6分钟(即360000000000纳秒),elapsed = 6m/1纳秒
> SELECT ELAPSED("water_level") FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:24:00Z'
name: h2o_feet
--------------
time                            elapsed
2015-08-18T00:06:00Z      360000000000
2015-08-18T00:12:00Z      360000000000
2015-08-18T00:18:00Z      360000000000
2015-08-18T00:24:00Z      360000000000
#每两个`water_level`数据记录直接的时间间隔是6分钟,elapsed = 6m/1m
> SELECT ELAPSED("water_level",1m) FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:24:00Z'
name: h2o_feet
--------------
time                            elapsed
2015-08-18T00:06:00Z      6
2015-08-18T00:12:00Z      6
2015-08-18T00:18:00Z      6
2015-08-18T00:24:00Z      6
#每两个`water_level`数据记录直接的时间间隔是6分钟,elapsed = 6m / 1h。因为1h>6m,所以数据为0
> SELECT ELAPSED("water_level",1h) FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:24:00Z'
name: h2o_feet
--------------
time                            elapsed
2015-08-18T00:06:00Z      0
2015-08-18T00:12:00Z      0
2015-08-18T00:18:00Z      0
2015-08-18T00:24:00Z      0

3.5 MOVING_AVERAGE()

说明:返回单个字段的连续window个字段值的滑动平均值。 字段类型必须为int64float64。算法找度娘
基础语法

SELECT MOVING_AVERAGE(,) FROM  [WHERE ]

高级语法

SELECT MOVING_AVERAGE((),) FROM  WHERE  GROUP BY time()

支持的函数包括:COUNT()
, MEAN()
, MEDIAN()
, SUM()
, FIRST()
,LAST()
, MIN()
, MAX()
, and PERCENTILE()

例子

> 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.116+2.064)/2`),第二条(`(2.028+2.116)/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
#分组统计
> 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.064+2.028)/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

3.6 NON_NEGATIVE_DERIVATIVE()

说明:返回系列中单个字段中的值的非负变化率。 InfluxDB计算时间域值之间的差异,并将这些结果转换为单位变化率。 单位参数是可选的,如果未指定,则默认为1秒(1秒)。改函数和DERIVATIVE()函数类似,只是返回其正数结果。
语法

SELECT NON_NEGATIVE_DERIVATIVE(, []) FROM  [WHERE ]

SELECT NON_NEGATIVE_DERIVATIVE(AGGREGATION_FUNCTION(),[]) FROM  WHERE  GROUP BY time()

4. 预测函数

4.1 HOLT_WINTERS()

说明:霍尔特指数平滑法,该字段必须是int64float64类型。算法找度娘。
语法

SELECT HOLT_WINTERS(FUNCTION(),,) FROM  WHERE  GROUP BY time()[,]

SELECT HOLT_WINTERS_WITH_FIT(FUNCTION(),,) FROM  WHERE  GROUP BY time()[,]

你可能感兴趣的:(InfuxDB函数说明)