官方文档
InfluxQL是一种类似SQL的查询语言,用于与InfluxDB中的数据交互。以下各节详细介绍了InfluxQL的SELECT
语句和用于探索数据的有用查询语法。
The Basics: | Configure Query Results: | General Tips on Query Syntax: |
---|---|---|
The SELECT statement | ORDER BY time DESC | Time Syntax |
The WHERE clause | The LIMIT and SLIMIT clauses | Regular Expressions |
The GROUP BY clause | The OFFSET and SOFFSET clauses | Data types and cast operations |
The INTO clause | The Time Zone clause | Merge behavior |
Multiple statements | ||
Subqueries |
SELECT
语句从特定的measurement或measurements中查询数据。
SELECT [,,] FROM [,]
The SELECT statement requires a SELECT clause and a FROM clause.
SELECT
子句SELECT子句支持几种指定数据的格式:
SELECT *
Returns all fields and tags.
SELECT "
Returns a specific field.
SELECT "
Returns more than one field.
SELECT "
Returns a specific field and tag. The SELECT clause must specify at least one field when it includes a tag.
SELECT "
Returns a specific field and tag. The ::[field | tag]
syntax specifies the identifier’s type. Use this syntax to differentiate between field keys and tag keys that have the same name.
FROM
子句SELECT子句支持几种指定数据的格式:
FROM
返回单个度量的数据。如果使用的是CLI influxdb,则查询所用数据库中的度量值和默认保留策略。如果使用influxdb API influxdb查询数据库中在db
query string参数和默认保留策略中指定的度量值。
FROM
Returns data from more than one measurement.
FROM
从完全限定的度量返回数据。通过指定度量值的数据库和保留策略来完全限定度量值。
FROM
返回用户指定数据库中默认保留测量的度量值的数据.
如果标识符包含除[A-z,0-9,u]以外的字符,如果它们以数字开头,或者如果它们是InfluxQL关键字,则必须使用双引号。虽然并不总是必要的,但我们建议您对标识符进行双引号。
注意:查询的引用语法与行协议不同。请查看查询中单引号和双引号的规则。
示例:
Select all fields and tags from a single measurement
> SELECT * FROM "h2o_feet"
name: h2o_feet
--------------
time level description location water_level
2015-08-18T00:00:00Z below 3 feet santa_monica 2.064
2015-08-18T00:00:00Z between 6 and 9 feet coyote_creek 8.12
[...]
2015-09-18T21:36:00Z between 3 and 6 feet santa_monica 5.066
2015-09-18T21:42:00Z between 3 and 6 feet santa_monica 4.938
如果您使用的是CLI,请确保在运行查询之前输入USE
NOAA_water_database。CLI查询所用数据库中的数据和默认保留策略。如果使用influxDB API,请确保将db
query string参数设置为NOAA_water_database数据库。如果未设置rp
query string参数,InfluxDB API将自动查询数据库的默认保留策略。
Select specific tags and fields from a single measurement
> SELECT "level description","location","water_level" FROM "h2o_feet"
name: h2o_feet
--------------
time level description location water_level
2015-08-18T00:00:00Z below 3 feet santa_monica 2.064
2015-08-18T00:00:00Z between 6 and 9 feet coyote_creek 8.12
[...]
2015-09-18T21:36:00Z between 3 and 6 feet santa_monica 5.066
2015-09-18T21:42:00Z between 3 and 6 feet santa_monica 4.938
The query selects the level description field, the location tag, and the water_level field.请注意,SELECT
子句在包含tag时必须至少指定一个field。
Select specific tags and fields from a single measurement, and provide their identifier type
> SELECT "level description"::field,"location"::tag,"water_level"::field FROM "h2o_feet"
name: h2o_feet
--------------
time level description location water_level
2015-08-18T00:00:00Z below 3 feet santa_monica 2.064
2015-08-18T00:00:00Z between 6 and 9 feet coyote_creek 8.12
[...]
2015-09-18T21:36:00Z between 3 and 6 feet santa_monica 5.066
2015-09-18T21:42:00Z between 3 and 6 feet santa_monica 4.938
The query selects the level description field, the location tag, and the water_level field from the h2o_feet measurement.::[field | tag]
语法指定标识符是字段还是标记。使用::[field | tag]
区分相同的字段键和标记键。大多数用例都不需要这种语法。
Select all fields from a single measurement
> SELECT *::field FROM "h2o_feet"
name: h2o_feet
--------------
time level description water_level
2015-08-18T00:00:00Z below 3 feet 2.064
2015-08-18T00:00:00Z between 6 and 9 feet 8.12
[...]
2015-09-18T21:36:00Z between 3 and 6 feet 5.066
2015-09-18T21:42:00Z between 3 and 6 feet 4.938
The query selects all fields from the h2o_feet measurement. The SELECT clause supports combining the * syntax with the :: syntax.
Select a specific field from a measurement and perform basic arithmetic
> SELECT ("water_level" * 2) + 4 FROM "h2o_feet"
name: h2o_feet
--------------
time water_level
2015-08-18T00:00:00Z 20.24
2015-08-18T00:00:00Z 8.128
[...]
2015-09-18T21:36:00Z 14.132
2015-09-18T21:42:00Z 13.876
The query multiplies water_level’s field values by two and adds four to those values. Note that InfluxDB follows the standard order of operations. See Mathematical Operators for more on supported operators.
Select all data from more than one measurement
> SELECT * FROM "h2o_feet","h2o_pH"
name: h2o_feet
--------------
time level description location pH water_level
2015-08-18T00:00:00Z below 3 feet santa_monica 2.064
2015-08-18T00:00:00Z between 6 and 9 feet coyote_creek 8.12
[...]
2015-09-18T21:36:00Z between 3 and 6 feet santa_monica 5.066
2015-09-18T21:42:00Z between 3 and 6 feet santa_monica 4.938
name: h2o_pH
------------
time level description location pH water_level
2015-08-18T00:00:00Z santa_monica 6
2015-08-18T00:00:00Z coyote_creek 7
[...]
2015-09-18T21:36:00Z santa_monica 8
2015-09-18T21:42:00Z santa_monica 7
查询从两个测量值中选择所有字段和标记:h2o_feet and h2o_pH.用逗号(,
)分隔多个测量值。
Select all data from a fully qualified measurement
从完全限定测量中查询所有数据
> SELECT * FROM "NOAA_water_database"."autogen"."h2o_feet"
name: h2o_feet
--------------
time level description location water_level
2015-08-18T00:00:00Z below 3 feet santa_monica 2.064
2015-08-18T00:00:00Z between 6 and 9 feet coyote_creek 8.12
[...]
2015-09-18T21:36:00Z between 3 and 6 feet santa_monica 5.066
2015-09-18T21:42:00Z between 3 and 6 feet santa_monica 4.938
查询选择NOAA_water_darabase中的数据、autogen保留策略和h2o_feet。
在CLI中,完全限定一个度量值,以查询使用的数据库以外的数据库和默认保留策略以外的保留策略中的数据。在InfluxDB API中,如果需要,完全限定度量值,而不是使用db
和rp
查询字符串参数。
Select all data from a measurement in a particular database
> SELECT * FROM "NOAA_water_database".."h2o_feet"
name: h2o_feet
--------------
time level description location water_level
2015-08-18T00:00:00Z below 3 feet santa_monica 2.064
2015-08-18T00:00:00Z between 6 and 9 feet coyote_creek 8.12
[...]
2015-09-18T21:36:00Z between 3 and 6 feet santa_monica 5.066
2015-09-18T21:42:00Z between 3 and 6 feet santa_monica 4.938
查询选择NOAA_water_darabase中的数据、默认保留策略和h2o_feet测量值。这个..
指示指定数据库的默认保留策略。
在CLI中,指定要在已用数据库以外的数据库中查询数据的数据库。在InfluxDB API中,如果需要,指定数据库以代替使用db
query string参数。
Selecting tag keys in the SELECT clause
查询要求SELECT子句中至少有一个字段键才能返回数据。如果SELECT子句只包含一个标记键或多个标记键,则查询返回空响应。此行为是系统存储数据方式的结果。
例子
以下查询不返回数据,因为它在SELECT子句中指定了单个标记键(位置):
> SELECT "location" FROM "h2o_feet"
若要返回任何和location
标签键相关联的数据,SELECT
查询语句至少包含一个字段键(water_level
):
> SELECT "water_level","location" FROM "h2o_feet"
name: h2o_feet
time water_level location
---- ----------- --------
2015-08-18T00:00:00Z 8.12 coyote_creek
2015-08-18T00:00:00Z 2.064 santa_monica
[...]
2015-09-18T21:36:00Z 5.066 santa_monica
2015-09-18T21:42:00Z 4.938 santa_monica
The WHERE filters data based on fields, tags, and/or timestamps.
SELECT_clause FROM_clause WHERE [(AND|OR) [...]]
The WHERE clause supports conditional_expressions on fields, tags, and timestamps.
注意InfluxDB不支持在
WHERE
子句中使用或指定多个时间范围。例如,InfluxDB将为以下查询返回空响应:
> SELECT * FROM "absolutismus" WHERE time = '2016-07-31T20:07:00Z' OR time = '2016-07-31T23:07:17Z'
field_key ['string' | boolean | float | integer]
WHERE子句支持string、boolean、float和integer字段值进行比较。
WHERE子句中使用单引号字符串字段值。使用非引号字符串字段值或双引号字符串字段值的查询不会返回任何数据,并且在大多数情况下不会返回错误。
Supported operators:
Operator | Meaning |
---|---|
= | equal to |
<> | not equal to |
!= | not equal to |
> | greater than |
>= | greater than or equal to |
< | less than |
<= | less than or equal to |
tag_key ['tag_value']
WHERE子句中使用单引号标签值(tag values)。使用非引号字符串字段值或双引号字符串字段值的查询不会返回任何数据,并且在大多数情况下不会返回错误。
Supported operators:
Operator | Meaning |
---|---|
= | equal to |
<> | not equal to |
!= | not equal to |
对于大多数SELECT
语句,默认时间范围是在1677-09-21 00:12:43.145224194
和2262-04-11T23:47:16.854775806Z UTC
之间。对于带有GROUP BY time()
子句的SELECT
语句,默认时间范围是1677-09-21 00:12:43.145224194
UTC和now()
之间。
此页上的Time语法部分详细说明如何在WHERE子句中指定可选时间范围。
Select data that have specific field key-values
> SELECT * FROM "h2o_feet" WHERE "water_level" > 8
name: h2o_feet
--------------
time level description location water_level
2015-08-18T00:00:00Z between 6 and 9 feet coyote_creek 8.12
2015-08-18T00:06:00Z between 6 and 9 feet coyote_creek 8.005
[...]
2015-09-18T00:12:00Z between 6 and 9 feet coyote_creek 8.189
2015-09-18T00:18:00Z between 6 and 9 feet coyote_creek 8.084
The query returns data from the h2o_feet measurement with field values of water_level that are greater than eight.
Select data that have a specific string field key-value
> SELECT * FROM "h2o_feet" WHERE "level description" = 'below 3 feet'
name: h2o_feet
--------------
time level description location water_level
2015-08-18T00:00:00Z below 3 feet santa_monica 2.064
2015-08-18T00:06:00Z below 3 feet santa_monica 2.116
[...]
2015-09-18T14:06:00Z below 3 feet santa_monica 2.999
2015-09-18T14:36:00Z below 3 feet santa_monica 2.907
The query returns data from the h2o_feet measurement with field values of level description that equal the below 3 feet string. InfluxQL requires single quotes around string field values in the WHERE clause.
Select data that have a specific field key-value and perform basic arithmetic
> SELECT * FROM "h2o_feet" WHERE "water_level" + 2 > 11.9
name: h2o_feet
--------------
time level description location water_level
2015-08-29T07:06:00Z at or greater than 9 feet coyote_creek 9.902
2015-08-29T07:12:00Z at or greater than 9 feet coyote_creek 9.938
2015-08-29T07:18:00Z at or greater than 9 feet coyote_creek 9.957
2015-08-29T07:24:00Z at or greater than 9 feet coyote_creek 9.964
2015-08-29T07:30:00Z at or greater than 9 feet coyote_creek 9.954
2015-08-29T07:36:00Z at or greater than 9 feet coyote_creek 9.941
2015-08-29T07:42:00Z at or greater than 9 feet coyote_creek 9.925
2015-08-29T07:48:00Z at or greater than 9 feet coyote_creek 9.902
2015-09-02T23:30:00Z at or greater than 9 feet coyote_creek 9.902
The query returns data from the h2o_feet measurement with field values of water_level plus two that are greater than 11.9. Note that InfluxDB follows the standard order of operations See Mathematical Operators for more on supported operators.
Select data that have a specific tag key-value
> SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica'
name: h2o_feet
--------------
time water_level
2015-08-18T00:00:00Z 2.064
2015-08-18T00:06:00Z 2.116
[...]
2015-09-18T21:36:00Z 5.066
2015-09-18T21:42:00Z 4.938
The query returns data from the h2o_feet measurement where the tag key location is set to santa_monica. InfluxQL requires single quotes around tag values in the WHERE clause.
Select data that have specific field key-values and tag key-values
> SELECT "water_level" FROM "h2o_feet" WHERE "location" <> 'santa_monica' AND (water_level < -0.59 OR water_level > 9.95)
name: h2o_feet
--------------
time water_level
2015-08-29T07:18:00Z 9.957
2015-08-29T07:24:00Z 9.964
2015-08-29T07:30:00Z 9.954
2015-08-29T14:30:00Z -0.61
2015-08-29T14:36:00Z -0.591
2015-08-30T15:18:00Z -0.594
The query returns data from the h2o_feet measurement where the tag key location is not set to santa_monica and where the field values of water_level are either less than -0.59 or greater than 9.95. The WHERE clause supports the operators AND and OR, and supports separating logic with parentheses.
Select data that have specific timestamps
> SELECT * FROM "h2o_feet" WHERE time > now() - 7d
WHERE子句查询意外地未返回任何数据
在大多数情况下,此问题是由于标记值(tag values)或字符串字段值(field values)周围缺少单引号造成的。带有无引号或双引号标记值或字符串字段值的查询不会返回任何数据,并且在大多数情况下不会返回错误。
下面代码块中的前两个查询尝试指定标记值santa_monica,不带任何引号,带双引号。这些查询不返回结果。第三个查询单引号santa_monica(这是支持的语法)并返回预期结果。
> SELECT "water_level" FROM "h2o_feet" WHERE "location" = santa_monica
> SELECT "water_level" FROM "h2o_feet" WHERE "location" = "santa_monica"
> SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica'
name: h2o_feet
--------------
time water_level
2015-08-18T00:00:00Z 2.064
[...]
2015-09-18T21:42:00Z 4.938
下面代码块中的前两个查询尝试指定不带任何引号和双引号的长度大于或等于9英尺的字符串字段值。第一个查询返回一个错误,因为字符串字段值包含空格。第二个查询不返回结果。第三个查询单引号位于或大于9英尺(这是支持的语法)并返回预期结果。
> SELECT "level description" FROM "h2o_feet" WHERE "level description" = at or greater than 9 feet
ERR: error parsing query: found than, expected ; at line 1, char 86
> SELECT "level description" FROM "h2o_feet" WHERE "level description" = "at or greater than 9 feet"
> SELECT "level description" FROM "h2o_feet" WHERE "level description" = 'at or greater than 9 feet'
name: h2o_feet
--------------
time level description
2015-08-26T04:00:00Z at or greater than 9 feet
[...]
2015-09-15T22:42:00Z at or greater than 9 feet
GROUPBY子句按用户指定的标记集或时间间隔对查询结果进行分组。
GROUP BY tags | |||
GROUP BY time intervals: | Basic Syntax | Advanced Syntax | GROUP BY time intervals and fill() |
GROUP BY
查询按用户指定的标记集对查询结果进行分组。
SELECT_clause FROM_clause [WHERE_clause] GROUP BY [* | [,
GROUP BY *
Groups results by all tags
GROUP BY
Groups results by a specific tag
GROUP BY
Groups results by more than one tag. The order of the tag keys is irrelevant.
如果查询包含WHERE子句,则GROUP BY子句必须出现在WHERE子句之后。
Group query results by a single tag
> SELECT MEAN("water_level") FROM "h2o_feet" GROUP BY "location"
name: h2o_feet
tags: location=coyote_creek
time mean
---- ----
1970-01-01T00:00:00Z 5.359342451341401
name: h2o_feet
tags: location=santa_monica
time mean
---- ----
1970-01-01T00:00:00Z 3.530863470081006
查询使用InfluxQL函数计算water_level测量中每个位置标记值的平均水位。InfluxDB返回两个series的结果:location的每个标记值对应一个序列。
注意:在InfluxDB中,epoch 0(1970-01-01T00:00:00Z)通常用作空时间戳等价物。如果请求的查询没有时间戳可返回,例如具有无限时间范围的聚合函数,InfluxDB将epoch 0作为时间戳返回。
Group query results by more than one tag
> SELECT MEAN("index") FROM "h2o_quality" GROUP BY "location","randtag"
name: h2o_quality
tags: location=coyote_creek, randtag=1
time mean
---- ----
1970-01-01T00:00:00Z 50.69033760186263
name: h2o_quality
tags: location=coyote_creek, randtag=2
time mean
---- ----
1970-01-01T00:00:00Z 49.661867544220485
name: h2o_quality
tags: location=coyote_creek, randtag=3
time mean
---- ----
1970-01-01T00:00:00Z 49.360939907550076
name: h2o_quality
tags: location=santa_monica, randtag=1
time mean
---- ----
1970-01-01T00:00:00Z 49.132712456344585
name: h2o_quality
tags: location=santa_monica, randtag=2
time mean
---- ----
1970-01-01T00:00:00Z 50.2937984496124
name: h2o_quality
tags: location=santa_monica, randtag=3
time mean
---- ----
1970-01-01T00:00:00Z 49.99919903884662
查询使用InfluxQL函数来计算h2o_quality
度量中location
标记和randtag
标记的每个组合的平均索引。在GROUPBY子句中用逗号分隔多个标记。
Group query results by all tags
> SELECT MEAN("index") FROM "h2o_quality" GROUP BY *
name: h2o_quality
tags: location=coyote_creek, randtag=1
time mean
---- ----
1970-01-01T00:00:00Z 50.55405446521169
name: h2o_quality
tags: location=coyote_creek, randtag=2
time mean
---- ----
1970-01-01T00:00:00Z 50.49958856271162
name: h2o_quality
tags: location=coyote_creek, randtag=3
time mean
---- ----
1970-01-01T00:00:00Z 49.5164137518956
name: h2o_quality
tags: location=santa_monica, randtag=1
time mean
---- ----
1970-01-01T00:00:00Z 50.43829082296367
name: h2o_quality
tags: location=santa_monica, randtag=2
time mean
---- ----
1970-01-01T00:00:00Z 52.0688508894012
name: h2o_quality
tags: location=santa_monica, randtag=3
time mean
---- ----
1970-01-01T00:00:00Z 49.29386362086556
查询使用InfluxQL函数来计算h2o_quality
度量中每个可能的标记组合的平均索引。
注意,查询结果与示例2中的查询结果相同,在示例2中,我们显式指定了location
和randtag
标签键。这是因为h2o_quality
测量只有两个标签键。
GROUP BY time()
查询按用户指定的时间间隔对查询结果进行分组。
SELECT () FROM_clause WHERE GROUP BY time(),[tag_key] [fill()]
Basic GROUP BY time()
查询需要SELECT
子句中的InfluxQL
函数和WHERE
子句中的时间范围。注意GROUP BY
子句必须在WHERE
子句之后。
time(time_interval)
GROUP BY time()
子句中的时间间隔是一个持续时间文本。它确定InfluxDB组随时间查询结果的方式。例如,在WHERE
子句中指定的时间范围内,将时间间隔为5m的组查询结果分成5分钟的时间组。
fill(
fill(
是可选的。它更改没有数据的时间间隔的报告值。
The examples below use the following subsample of the sample data:
> SELECT "water_level","location" FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z'
name: h2o_feet
--------------
time water_level location
2015-08-18T00:00:00Z 8.12 coyote_creek
2015-08-18T00:00:00Z 2.064 santa_monica
2015-08-18T00:06:00Z 8.005 coyote_creek
2015-08-18T00:06:00Z 2.116 santa_monica
2015-08-18T00:12:00Z 7.887 coyote_creek
2015-08-18T00:12:00Z 2.028 santa_monica
2015-08-18T00:18:00Z 7.762 coyote_creek
2015-08-18T00:18:00Z 2.126 santa_monica
2015-08-18T00:24:00Z 7.635 coyote_creek
2015-08-18T00:24:00Z 2.041 santa_monica
2015-08-18T00:30:00Z 7.5 coyote_creek
2015-08-18T00:30:00Z 2.051 santa_monica
Group query results into 12 minute intervals
> SELECT COUNT("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z' GROUP BY time(12m)
name: h2o_feet
--------------
time count
2015-08-18T00:00:00Z 2
2015-08-18T00:12:00Z 2
2015-08-18T00:24:00Z 2
查询使用InfluxQL函数计算water_level
点的数量,标记location=coyote_creek
,并将结果按12分钟的间隔分组。
每个时间戳的结果表示一个12分钟的间隔。第一个时间戳的计数包括2015-08-18T00:00:00Z
到2015-08-18T00:12:00Z
之间的原始数据,但不包括上限;第二个时间戳的计数包括2015-08-18T00:12:00Z
到2015-08-18T00:24:00Z
之间的原始数据,但不包括上限。
Group query results into 12 minutes intervals and by a tag key
> SELECT COUNT("water_level") FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z' GROUP BY time(12m),"location"
name: h2o_feet
tags: location=coyote_creek
time count
---- -----
2015-08-18T00:00:00Z 2
2015-08-18T00:12:00Z 2
2015-08-18T00:24:00Z 2
name: h2o_feet
tags: location=santa_monica
time count
---- -----
2015-08-18T00:00:00Z 2
2015-08-18T00:12:00Z 2
2015-08-18T00:24:00Z 2
查询使用InfluxQL函数计算water_level
点的数量。它按location
标签将结果分组,并以12分钟为间隔。注意,时间间隔和标记键在GROUPBY子句中用逗号分隔。
查询返回两系列结果:location
标记的每个标记值一个。每个时间戳的结果表示一个12分钟的间隔。第一个时间戳的计数包括2015-08-18T00:00:00Z
到2015-08-18T00:12:00Z
之间的原始数据,但不包括后者在内;第二个时间戳的计数包括2015-08-18T00:12:00Z
到2015-08-18T00:24:00Z
之间的原始数据,但不包括后者在内。
Common issues with basic syntax
查询结果中意外的时间戳和值
使用基本语法,infloxdb依赖于GROUP BY time()
间隔和系统预设的时间边界来确定每个时间间隔中包含的原始数据和查询返回的时间戳。在某些情况下,这可能会导致意想不到的结果。
例如:
RAW data:
> SELECT "water_level" FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:18:00Z'
name: h2o_feet
--------------
time water_level
2015-08-18T00:00:00Z 8.12
2015-08-18T00:06:00Z 8.005
2015-08-18T00:12:00Z 7.887
2015-08-18T00:18:00Z 7.762
以下查询包含12分钟的时间范围,并将结果分组为12分钟的时间间隔,但它返回两个结果:
> SELECT COUNT("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-08-18T00:06:00Z' AND time < '2015-08-18T00:18:00Z' GROUP BY time(12m)
name: h2o_feet
time count
---- -----
2015-08-18T00:00:00Z 1 <----- Note that this timestamp occurs before the start of the query's time range
2015-08-18T00:12:00Z 1
说明:
InfluxDB使用预设的整数时间边界来按间隔分组,这些间隔独立于WHERE
子句中的任何时间条件。在计算结果时,所有返回的数据都必须出现在查询的显式时间范围内,但按间隔分组将基于预设的时间边界。
下表显示了预设的时间边界、相关的GROUP BY time()
间隔、包含的点以及结果中每个GROUP BY time()
间隔返回的时间戳。
Time Interval Number | Preset Time Boundary | GROUP BY time() Interval | Points Included | Returned Timestamp |
---|---|---|---|---|
1 | time >= 2015-08-18T00:00:00Z AND time < 2015-08-18T00:12:00Z | time >= 2015-08-18T00:06:00Z AND time < 2015-08-18T00:12:00Z | 8.005 | 2015-08-18T00:00:00Z |
2 | time >= 2015-08-12T00:12:00Z AND time < 2015-08-18T00:24:00Z | time >= 2015-08-12T00:12:00Z AND time < 2015-08-18T00:18:00Z | 7.887 | 2015-08-18T00:12:00Z |
第一个预设的12分钟时间边界从00:00开始,在00:12之前结束。只有一个原始点(8.005)同时位于查询的第一个GROUP BY time()
间隔内和第一个时间边界内。注意,虽然返回的时间戳发生在查询时间范围的开始之前,但查询结果不包括发生在查询时间范围之前的数据。
第二个预设的12分钟时间边界从00:12开始,在00:24之前结束。只有一个原始点(7.887)同时位于查询的第二组GROUP BY time()
间隔内和第二个时间边界内。
高级的GROUP BY time()
语法允许用户移动InfluxDB数据库预设时间边界的开始时间。高级语法部分的示例3继续执行此处显示的查询;它将预设的时间边界向前移动6分钟,以便InfluxDB返回:
name: h2o_feet
time count
---- -----
2015-08-18T00:06:00Z 2
SELECT () FROM_clause WHERE GROUP BY time(,),[tag_key] [fill()]
高级GROUP BY time()
查询要求在SELECT
子句中使用influxQL函数,在WHERE
子句中使用时间范围。注意GROUP BY
子句必须在WHERE
子句之后。
time(time_interval,offset_interval)
有关time_interval
的详细信息,请参见Basic GROUP BY time()语法。
offset_interval
是持续时间文本。它向前或向后移动InfluxDB数据库的预设时间边界。offset_interval
可以为正或负。
fill(
fill(
是可选的。它更改没有数据的时间间隔的报告值。
总之:
高级的GROUP BY time()
查询依赖于time_interval,
、offset_interval
和InfluxDB数据库的预设时间边界来确定每个时间间隔中包含的原始数据和查询返回的时间戳。
Examples of advanced syntax
> SELECT "water_level" FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:54:00Z'
name: h2o_feet
--------------
time water_level
2015-08-18T00:00:00Z 8.12
2015-08-18T00:06:00Z 8.005
2015-08-18T00:12:00Z 7.887
2015-08-18T00:18:00Z 7.762
2015-08-18T00:24:00Z 7.635
2015-08-18T00:30:00Z 7.5
2015-08-18T00:36:00Z 7.372
2015-08-18T00:42:00Z 7.234
2015-08-18T00:48:00Z 7.11
2015-08-18T00:54:00Z 6.982
Group query results into 18 minute intervals and shift the preset time boundaries forward
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-08-18T00:06:00Z' AND time <= '2015-08-18T00:54:00Z' GROUP BY time(18m,6m)
name: h2o_feet
time mean
---- ----
2015-08-18T00:06:00Z 7.884666666666667
2015-08-18T00:24:00Z 7.502333333333333
2015-08-18T00:42:00Z 7.108666666666667
查询使用InfluxQL函数计算平均water_level
,将结果分组为18分钟的时间间隔,并将预设的时间界限偏移6分钟。
没有偏移间隔的查询的时间边界和返回的时间戳遵循InfluxDB数据库的预设时间边界。我们首先检查不带偏移量的结果:
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-08-18T00:06:00Z' AND time <= '2015-08-18T00:54:00Z' GROUP BY time(18m)
name: h2o_feet
time mean
---- ----
2015-08-18T00:00:00Z 7.946
2015-08-18T00:18:00Z 7.6323333333333325
2015-08-18T00:36:00Z 7.238666666666667
2015-08-18T00:54:00Z 6.982
Time Interval Number | Preset Time Boundary | GROUP BY time() Interval | Points Included | Returned Timestamp |
---|---|---|---|---|
1 | time >= 2015-08-18T00:00:00Z AND time < 2015-08-18T00:18:00Z | time >= 2015-08-18T00:06:00Z AND time < 2015-08-18T00:18:00Z | 8.005,7.887 | 2015-08-18T00:00:00Z |
2 | time >= 2015-08-18T00:18:00Z AND time < 2015-08-18T00:36:00Z | <— same | 7.762,7.635,7.5 | 2015-08-18T00:18:00Z |
3 | time >= 2015-08-18T00:36:00Z AND time < 2015-08-18T00:54:00Z | <— same | 7.372,7.234,7.11 | 2015-08-18T00:36:00Z |
4 | time >= 2015-08-18T00:54:00Z AND time < 2015-08-18T01:12:00Z | time = 2015-08-18T00:54:00Z | 6.982 | 2015-08-18T00:54:00Z |
第一个预设的18分钟时间边界从00:00开始,在00:18之前结束。两个原始点(8.005和7.887)都位于第一组GROUP BY time()
间隔内和第一时间边界内。注意,虽然返回的时间戳发生在查询时间范围的开始之前,但查询结果不包括发生在查询时间范围之前的数据。
第二个预设的18分钟时间边界从00:18开始,在00:36之前结束。三个原始点(7.762、7.635和7.5)都在第二组GROUP BY time()
间隔内和第二个时间边界内。在这种情况下,边界时间范围和间隔的时间范围是相同的。
第四个预设的18分钟时间边界从00:54开始,在1:12:00之前结束。一个原始点(6.982)同时落在第四组GROUP BY time()
间隔内和第四个时间边界内。
具有offset_interval
的查询的时间边界和返回的时间戳遵循偏移时间边界:
Time Interval Number | Offset Time Boundary | GROUP BY time() Interval | Points Included | Returned Timestamp |
---|---|---|---|---|
1 | time >= 2015-08-18T00:06:00Z AND time < 2015-08-18T00:24:00Z | <— same | 8.005,7.887,7.762 | 2015-08-18T00:06:00Z |
2 | time >= 2015-08-18T00:24:00Z AND time < 2015-08-18T00:42:00Z | <— same | 7.635,7.5,7.372 | 2015-08-18T00:24:00Z |
3 | time >= 2015-08-18T00:42:00Z AND time < 2015-08-18T01:00:00Z | <— same | 7.234,7.11,6.982 | 2015-08-18T00:42:00Z |
4 | time >= 2015-08-18T01:00:00Z AND time < 2015-08-18T01:18:00Z | NA | NA | NA |
六分钟偏移间隔向前移动预设边界的时间范围,使得边界时间范围和相关的GROUP BY time()
间隔时间范围始终相同。使用偏移量,每个间隔对三个点执行计算,返回的时间戳与边界时间范围的开始和GROUP BY time()
间隔时间范围的开始都匹配。
请注意,offset_interval
强制第四个时间边界位于查询的时间范围之外,因此查询不返回最后一个时间间隔的结果。
Group query results into 12 minute intervals and shift the preset time boundaries back
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-08-18T00:06:00Z' AND time <= '2015-08-18T00:54:00Z' GROUP BY time(18m,-12m)
name: h2o_feet
time mean
---- ----
2015-08-18T00:06:00Z 7.884666666666667
2015-08-18T00:24:00Z 7.502333333333333
2015-08-18T00:42:00Z 7.108666666666667
查询使用InfluxQL函数计算平均water_level
,将结果分组为18分钟的时间间隔,并将预设的时间界限偏移-12分钟。
注意:示例2中的查询返回与示例1中的查询相同的结果,但示例2中的查询使用负偏移间隔而不是正偏移间隔。两个查询之间没有性能差异;在确定正偏移间隔和负偏移间隔时,可以自由选择最直观的选项。
没有偏移间隔的查询的时间边界和返回的时间戳遵循InfluxDB数据库的预设时间边界。我们首先检查不带偏移量的结果:
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-08-18T00:06:00Z' AND time <= '2015-08-18T00:54:00Z' GROUP BY time(18m)
name: h2o_feet
time mean
---- ----
2015-08-18T00:00:00Z 7.946
2015-08-18T00:18:00Z 7.6323333333333325
2015-08-18T00:36:00Z 7.238666666666667
2015-08-18T00:54:00Z 6.982
没有偏移间隔的查询的时间边界和返回的时间戳遵循InfluxDB数据库的预设时间边界:
Time Interval Number | Preset Time Boundary | GROUP BY time() Interval | Points Included | Returned Timestamp |
---|---|---|---|---|
1 | time >= 2015-08-18T00:00:00Z AND time < 2015-08-18T00:18:00Z | time >= 2015-08-18T00:06:00Z AND time < 2015-08-18T00:18:00Z | 8.005,7.887 | 2015-08-18T00:00:00Z |
2 | time >= 2015-08-18T00:18:00Z AND time < 2015-08-18T00:36:00Z | <— same | 7.762,7.635,7.5 | 2015-08-18T00:18:00Z |
3 | time >= 2015-08-18T00:36:00Z AND time < 2015-08-18T00:54:00Z | <— same | 7.372,7.234,7.11 | 2015-08-18T00:36:00Z |
4 | time >= 2015-08-18T00:54:00Z AND time < 2015-08-18T01:12:00Z | time = 2015-08-18T00:54:00Z | 6.982 | 2015-08-18T00:54:00Z |
第一个预设的18分钟时间边界从00:00开始,在00:18之前结束。两个原始点(8.005和7.887)都位于第一组GROUP BY time()
间隔内和第一时间边界内。注意,虽然返回的时间戳发生在查询时间范围的开始之前,但查询结果不包括发生在查询时间范围之前的数据。第二个预设的18分钟时间边界从00:18开始,在00:36之前结束。三个原始点(7.762、7.635和7.5)都在第二组GROUP BY time()
间隔内和第二个时间边界内。在这种情况下,边界时间范围和间隔的时间范围是相同的。第四个预设的18分钟时间边界从00:54开始,在1:12:00之前结束。一个原始点(6.982)同时落在第四组GROUP BY time()
间隔内和第四个时间边界内。具有偏移间隔的查询的时间边界和返回的时间戳遵循偏移时间边界:
Time Interval Number | Offset Time Boundary | GROUP BY time() Interval | Points Included | Returned Timestamp |
---|---|---|---|---|
1 | time >= 2015-08-17T23:48:00Z AND time < 2015-08-18T00:06:00Z | NA | NA | NA |
2 | time >= 2015-08-18T00:06:00Z AND time < 2015-08-18T00:24:00Z | <— same | 8.005,7.887,7.762 | 2015-08-18T00:06:00Z |
3 | time >= 2015-08-18T00:24:00Z AND time < 2015-08-18T00:42:00Z | <— same | 7.635,7.5,7.372 | 2015-08-18T00:24:00Z |
4 | time >= 2015-08-18T00:42:00Z AND time < 2015-08-18T01:00:00Z | <— same | 7.234,7.11,6.982 | 2015-08-18T00:42:00Z |
负的12分钟偏移间隔向后移动预设边界的时间范围,以便边界时间范围和相关的GROUP BY time()
间隔时间范围始终相同。使用偏移量,每个间隔对三个点执行计算,返回的时间戳与边界时间范围的开始和GROUP BY time()
间隔时间范围的开始都匹配。
请注意,offset_interval
强制第一个时间边界位于查询的时间范围之外,因此查询不返回该第一个时间间隔的结果。
将查询结果分组为12分钟的间隔,并将预设的时间界限前移
此示例是基本语法常见问题中概述的场景的延续。
> SELECT COUNT("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-08-18T00:06:00Z' AND time < '2015-08-18T00:18:00Z' GROUP BY time(12m,6m)
name: h2o_feet
time count
---- -----
2015-08-18T00:06:00Z 2
查询使用InfluxQL函数计算water_level
点的数量,将结果分组为12分钟的时间间隔,并将预设的时间界限偏移6分钟。
没有偏移间隔的查询的时间边界和返回的时间戳遵循InfluxDB数据库的预设时间边界。我们首先检查不带偏移量的结果:
Time Interval Number | Preset Time Boundary | GROUP BY time() Interval | Points Included | Returned Timestamp |
---|---|---|---|---|
1 | time >= 2015-08-18T00:00:00Z AND time < 2015-08-18T00:12:00Z | time >= 2015-08-18T00:06:00Z AND time < 2015-08-18T00:12:00Z | 8.005 | 2015-08-18T00:00:00Z |
2 | time >= 2015-08-12T00:12:00Z AND time < 2015-08-18T00:24:00Z | time >= 2015-08-12T00:12:00Z AND time < 2015-08-18T00:18:00Z | 7.887 | 2015-08-18T00:12:00Z |
第一个预设的12分钟时间边界从00:00开始,在00:12之前结束。只有一个原始点(8.005)同时位于查询的第一个GROUP BY time()
间隔内和第一个时间边界内。注意,虽然返回的时间戳发生在查询时间范围的开始之前,但查询结果不包括发生在查询时间范围之前的数据。
第二个预设的12分钟时间边界从00:12开始,在00:24之前结束。只有一个原始点(7.887)同时位于查询的第二组GROUP BY time()
间隔内和第二个时间边界内。
具有偏移间隔的查询的时间边界和返回的时间戳遵循偏移时间边界:
Time Interval Number | Offset Time Boundary | GROUP BY time() Interval |
Points Included | Returned Timestamp |
---|---|---|---|---|
1 | time >= 2015-08-18T00:06:00Z AND time < 2015-08-18T00:18:00Z |
<— same | 8.005 ,7.887 |
2015-08-18T00:06:00Z |
2 | time >= 2015-08-18T00:18:00Z AND time < 2015-08-18T00:30:00Z |
NA | NA | NA |
六分钟偏移间隔向前移动预设边界的时间范围,使得预设边界时间范围和相关的GROUP BY time()
间隔时间范围相同。使用偏移量,查询返回单个结果,返回的时间戳与边界时间范围的开始和GROUP BY time()
interval时间范围的开始都匹配。
请注意,offset_interval
强制第二个时间边界位于查询的时间范围之外,因此查询不返回该第二个时间间隔的结果。
fill()更改没有数据的时间间隔的报告值。
SELECT () FROM_clause WHERE GROUP BY time(time_interval,[)]
默认情况下,没有数据的GROUP By time()
间隔在输出列中报告空值。fill()
更改没有数据的时间间隔的报告值。注意,如果要按多个内容(例如,标记和时间间隔)分组,fill()
必须位于GROUP BY
子句的末尾。
填充选项
Any numerical value: 在没有数据的情况下报告时间间隔的给定数值。
linear
报告没有数据的时间间隔的线性插值结果。
none
没有时间戳和没有数据的时间间隔值。
null
对于没有数据的时间间隔,报告null,但返回时间戳。这与默认行为相同。
previous
报告没有数据的时间间隔的上一个时间间隔的值。
示例
Without fill(100):
> SELECT MAX("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-09-18T16:00:00Z' AND time <= '2015-09-18T16:42:00Z' GROUP BY time(12m)
name: h2o_feet
--------------
time max
2015-09-18T16:00:00Z 3.599
2015-09-18T16:12:00Z 3.402
2015-09-18T16:24:00Z 3.235
2015-09-18T16:36:00Z
With fill(100):
> SELECT MAX("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-09-18T16:00:00Z' AND time <= '2015-09-18T16:42:00Z' GROUP BY time(12m) fill(100)
name: h2o_feet
--------------
time max
2015-09-18T16:00:00Z 3.599
2015-09-18T16:12:00Z 3.402
2015-09-18T16:24:00Z 3.235
2015-09-18T16:36:00Z 100
fill(100)
将没有数据的时间间隔的报告值更改为100。
Without fill(linear):
> SELECT MEAN("tadpoles") FROM "pond" WHERE time >= '2016-11-11T21:00:00Z' AND time <= '2016-11-11T22:06:00Z' GROUP BY time(12m)
name: pond
time mean
---- ----
2016-11-11T21:00:00Z 1
2016-11-11T21:12:00Z
2016-11-11T21:24:00Z 3
2016-11-11T21:36:00Z
2016-11-11T21:48:00Z
2016-11-11T22:00:00Z 6
With fill(linear):
> SELECT MEAN("tadpoles") FROM "pond" WHERE time >= '2016-11-11T21:00:00Z' AND time <= '2016-11-11T22:06:00Z' GROUP BY time(12m) fill(linear)
name: pond
time mean
---- ----
2016-11-11T21:00:00Z 1
2016-11-11T21:12:00Z 2
2016-11-11T21:24:00Z 3
2016-11-11T21:36:00Z 4
2016-11-11T21:48:00Z 5
2016-11-11T22:00:00Z 6
fill(linear)
在没有数据的情况下将为时间间隔报告的值更改为线性插值的结果。
注:示例2中的数据不在NOAA_water_database
中。我们必须用不太规则的数据创建一个数据集来处理fill(linear)
。
Without fill(none):
> SELECT MAX("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-09-18T16:00:00Z' AND time <= '2015-09-18T16:42:00Z' GROUP BY time(12m)
name: h2o_feet
--------------
time max
2015-09-18T16:00:00Z 3.599
2015-09-18T16:12:00Z 3.402
2015-09-18T16:24:00Z 3.235
2015-09-18T16:36:00Z
With fill(none):
> SELECT MAX("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-09-18T16:00:00Z' AND time <= '2015-09-18T16:42:00Z' GROUP BY time(12m) fill(none)
name: h2o_feet
--------------
time max
2015-09-18T16:00:00Z 3.599
2015-09-18T16:12:00Z 3.402
2015-09-18T16:24:00Z 3.235
fill(none)
在没有数据的时间间隔内报告没有值和时间戳。
Without fill(null)
:
> SELECT MAX("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-09-18T16:00:00Z' AND time <= '2015-09-18T16:42:00Z' GROUP BY time(12m)
name: h2o_feet
--------------
time max
2015-09-18T16:00:00Z 3.599
2015-09-18T16:12:00Z 3.402
2015-09-18T16:24:00Z 3.235
2015-09-18T16:36:00Z
With fill(null)
:
> SELECT MAX("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-09-18T16:00:00Z' AND time <= '2015-09-18T16:42:00Z' GROUP BY time(12m) fill(null)
name: h2o_feet
--------------
time max
2015-09-18T16:00:00Z 3.599
2015-09-18T16:12:00Z 3.402
2015-09-18T16:24:00Z 3.235
2015-09-18T16:36:00Z
fill(null)
报告空值作为没有数据的时间间隔的值。该结果与不带fill(null)
的查询结果相同。
Without fill(previous)
:
> SELECT MAX("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-09-18T16:00:00Z' AND time <= '2015-09-18T16:42:00Z' GROUP BY time(12m)
name: h2o_feet
--------------
time max
2015-09-18T16:00:00Z 3.599
2015-09-18T16:12:00Z 3.402
2015-09-18T16:24:00Z 3.235
2015-09-18T16:36:00Z
With fill(previous)
:
> SELECT MAX("water_level") FROM "h2o_feet" WHERE "location"='coyote_creek' AND time >= '2015-09-18T16:00:00Z' AND time <= '2015-09-18T16:42:00Z' GROUP BY time(12m) fill(previous)
name: h2o_feet
--------------
time max
2015-09-18T16:00:00Z 3.599
2015-09-18T16:12:00Z 3.402
2015-09-18T16:24:00Z 3.235
2015-09-18T16:36:00Z 3.235
fill(previous)
将为没有数据的时间间隔报告的值更改为3.235,即上一个时间间隔的值。
在查询的时间范围内没有数据时使用fill()
进行查询
目前,如果查询的时间范围内没有数据,查询将忽略fill()
。这是预期的行为。GitHub上的一个开放特性请求建议fill()
应该强制返回值,即使查询的时间范围不包含任何数据。
以下查询不返回数据,因为water_level
在查询的时间范围内没有点。注意fill(800)对查询结果没有影响。
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location" = 'coyote_creek' AND time >= '2015-09-18T22:00:00Z' AND time <= '2015-09-18T22:18:00Z' GROUP BY time(12m) fill(800)
>
当上一个结果超出查询的时间范围时,使用fill(previous)
查询
如果前一个值超出查询的时间范围,则fill(previous)
不会在时间间隔内填充结果。
以下查询涵盖2015-09-18T16:24:00Z
到2015-09-18T16:54:00Z
之间的时间范围。请注意,fill(previous)
用2015-09-18T16:36:00Z
的结果填充2015-09-18T16:24:00Z
的结果。
> SELECT MAX("water_level") FROM "h2o_feet" WHERE location = 'coyote_creek' AND time >= '2015-09-18T16:24:00Z' AND time <= '2015-09-18T16:54:00Z' GROUP BY time(12m) fill(previous)
name: h2o_feet
--------------
time max
2015-09-18T16:24:00Z 3.235
2015-09-18T16:36:00Z 3.235
2015-09-18T16:48:00Z 4
下一个查询缩短了上一个查询中的时间范围。现在它涵盖了从2015-09-18T16:36:00Z
到2015-09-18T16:54:00Z
之间的时间。请注意,fill(previous)
不会将2015-09-18T16:36:00Z
的结果填充为2015-09-18T16:24:00Z
的结果;2015-09-18T16:24:00Z
的结果不在查询的较短时间范围内。
> SELECT MEAN("tadpoles") FROM "pond" WHERE time >= '2016-11-11T21:36:00Z' AND time <= '2016-11-11T22:06:00Z' GROUP BY time(12m) fill(linear)
name: pond
time mean
---- ----
2016-11-11T21:36:00Z
2016-11-11T21:48:00Z
2016-11-11T22:00:00Z 6
注:问题3中的数据不在NOAA_water_database。我们必须用不太规则的数据创建一个数据集来处理
fill(previous)
。
INTO
子句将查询结果写入用户指定的度量。
SELECT_clause INTO FROM_clause [WHERE_clause] [GROUP_BY_clause]
INTO
子句支持几种用于指定度量值的格式:
INTO
将数据写入指定的度量。如果使用的是CLI InflouxDB,则会将数据写入所用数据库中的度量值和默认保留策略。如果使用InfluxDB API,InfluxDB会将数据写入db
query string参数和默认保留策略中指定的数据库中的度量值。
INTO
将数据写入完全限定的度量。通过指定度量值的数据库和保留策略来完全限定度量值。
INTO
:将数据写入用户指定数据库中的度量值和默认保留策略。
INTO
将数据写入用户指定数据库中与FROM
子句中的正则表达式匹配的所有度量。:MEASUREMENT
是对FROM子
句中匹配的每个度量的回溯引用。
示例
Rename a database
> SELECT * INTO "copy_NOAA_water_database"."autogen".:MEASUREMENT FROM "NOAA_water_database"."autogen"./.*/ GROUP BY *
name: result
time written
---- -------
0 76290
不能在InfluxDB中直接重命名数据库,因此INTO
子句的一个常见用途是将数据从一个数据库移动到另一个数据库。上述查询将NOAA_water_database
和autogen
保留策略中的所有数据写入copy_NOAA_water_ddatabase
和autogen
保留策略。
backreference语法(:MEASUREMENT
)维护目标数据库中的源度量名称。请注意,在运行INTO
查询之前,copy_NOAA_water_database
及其autogen
保留策略都必须存在。有关如何管理数据库和保留策略,请参阅数据库管理。
GROUP BY *
子句将源数据库中的标记保留为目标数据库中的标记。以下查询不维护标记的序列上下文;标记将作为字段存储在目标数据库中(copy_NOAA_water_database):
SELECT * INTO "copy_NOAA_water_database"."autogen".:MEASUREMENT FROM "NOAA_water_database"."autogen"./.*/
当移动大量数据时,我们建议按顺序运行到不同度量的查询中,并在WHERE
子句中使用时间边界。这可以防止系统内存不足。下面的代码块提供了这些查询的示例语法:
SELECT *
INTO ..
FROM ..
WHERE time > now() - 100w AND time < now() - 90w GROUP BY *
SELECT *
INTO ..
FROM ..}
WHERE time > now() - 90w AND < now() - 80w GROUP BY *
SELECT *
INTO ..
FROM ..
WHERE time > now() - 80w AND time < now() - 70w GROUP BY *
Write the results of a query to a measurement
> SELECT "water_level" INTO "h2o_feet_copy_1" FROM "h2o_feet" WHERE "location" = 'coyote_creek'
name: result
------------
time written
1970-01-01T00:00:00Z 7604
> SELECT * FROM "h2o_feet_copy_1"
name: h2o_feet_copy_1
---------------------
time water_level
2015-08-18T00:00:00Z 8.12
[...]
2015-09-18T16:48:00Z 4
查询将其结果写入一个新的度量值:h2o_feet_copy_1
。如果您使用的是CLI,InfluxDB会将数据写入使用的数据库和默认的保留策略。如果您使用的是InfluxDB API,InfluxDB会将数据写入数据库,并在db
和rp
查询字符串参数中指定保留策略。如果未设置rp
query string参数,InfluxDB API会自动将数据写入数据库的默认保留策略。
响应显示InfluxDB写入h2o_feet_copy_1
点数(7605)。响应中的时间戳没有意义;InfluxDB使用epoch 0(1970-01-01T00:00:00Z)作为空时间戳等价物。
Write the results of a query to a fully qualified measurement
> SELECT "water_level" INTO "where_else"."autogen"."h2o_feet_copy_2" FROM "h2o_feet" WHERE "location" = 'coyote_creek'
name: result
------------
time written
1970-01-01T00:00:00Z 7604
> SELECT * FROM "where_else"."autogen"."h2o_feet_copy_2"
name: h2o_feet_copy_2
---------------------
time water_level
2015-08-18T00:00:00Z 8.12
[...]
2015-09-18T16:48:00Z 4
查询将其结果写入一个新的测量值:h2o_-feet_-copy_2
。InfluxDB 将数据写入where else
数据库和autogen保留策略。请注意,在运行INTO
查询之前,where-else
和autogen
都必须存在。有关如何管理数据库和保留策略,请参阅数据库管理。
响应显示InfluxDB写入h2o_feet_copy_2
的点数(7605)。响应中的时间戳没有意义;InfluxDB使用epoch 0(1970-01-01T00:00:00Z)作为空时间戳等价物。
Write aggregated results to a measurement (downsampling)
> SELECT MEAN("water_level") INTO "all_my_averages" FROM "h2o_feet" WHERE "location" = 'coyote_creek' AND time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z' GROUP BY time(12m)
name: result
------------
time written
1970-01-01T00:00:00Z 3
> SELECT * FROM "all_my_averages"
name: all_my_averages
---------------------
time mean
2015-08-18T00:00:00Z 8.0625
2015-08-18T00:12:00Z 7.8245
2015-08-18T00:24:00Z 7.5675
查询使用InfluxQL函数和GROUP BY time()
子句聚合数据。它还将其结果写入“all_my_averages
”测量。响应显示InfluxDB写入所有平均值的点数(3)。响应中的时间戳没有意义;InflUxDB使用epoch 0(1970-01-01T00:00:00Z)作为空时间戳等价物。查询是降采样的一个例子:获取高精度数据,将这些数据聚合到低精度,并将低精度数据存储在数据库中。降采样是INTO
子句的常见用例。
Write aggregated results for more than one measurement to a different database (downsampling with backreferencing)
> SELECT MEAN(*) INTO "where_else"."autogen".:MEASUREMENT FROM /.*/ WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:06:00Z' GROUP BY time(12m)
name: result
time written
---- -------
1970-01-01T00:00:00Z 5
> SELECT * FROM "where_else"."autogen"./.*/
name: average_temperature
time mean_degrees mean_index mean_pH mean_water_level
---- ------------ ---------- ------- ----------------
2015-08-18T00:00:00Z 78.5
name: h2o_feet
time mean_degrees mean_index mean_pH mean_water_level
---- ------------ ---------- ------- ----------------
2015-08-18T00:00:00Z 5.07625
name: h2o_pH
time mean_degrees mean_index mean_pH mean_water_level
---- ------------ ---------- ------- ----------------
2015-08-18T00:00:00Z 6.75
name: h2o_quality
time mean_degrees mean_index mean_pH mean_water_level
---- ------------ ---------- ------- ----------------
2015-08-18T00:00:00Z 51.75
name: h2o_temperature
time mean_degrees mean_index mean_pH mean_water_level
---- ------------ ---------- ------- ----------------
2015-08-18T00:00:00Z 63.75
查询使用InfluxQL函数和GROUP BY time()
子句聚合数据。它在与FROM子句中的正则表达式匹配的每个度量中聚合数据,并将结果写入where_ else
数据库和autogen
保留策略中同名的度量。请注意,在运行INTO
查询之前,where_else
和autogen
都必须存在。有关如何管理数据库和保留策略,请参阅数据库管理。
响应显示InfluxDB写入where_else
数据库的点数(5)和autogen
保留策略。响应中的时间戳没有意义;InfluxDB使用epoch 0(1970-01-01T00:00:00Z)作为空时间戳等价物。
该查询是使用反向引用进行降采样的示例。它从多个测量中获取高精度数据,将这些数据聚合到低精度,并将低精度数据存储在数据库中。带反向引用的降采样是INTO子句的常见用例。
丢失数据
如果INTO
查询在SELECT
子句中包含标记键,则查询会将当前度量中的标记转换为目标度量中的字段。这可能会导致InfluxDB覆盖先前由标记值区分的点。请注意,此行为不适用于使用TOP()
或BOTTOM()
函数的查询。常见问题文档详细描述了这种行为。
若要将当前度量中的标记保留为目标度量中的标记,请按相关标记键分组,或在“INTO
”查询中按GROUP BY *
分组。
Automating queries with the INTO clause
The INTO clause section in this document shows how to manually implement queries with an INTO clause. See the Continuous Queries documentation for how to automate INTO clause queries on realtime data. Among other uses, Continuous Queries automate the downsampling process.
默认情况下,InfluxDB按时间升序返回结果;返回的第一个点具有最早的时间戳,返回的最后一个点具有最新的时间戳。ORDER BY time DESC
反转该顺序,以便InfluxDB首先返回带有最新时间戳的点。
SELECT_clause [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] ORDER BY time DESC
如果查询包含GROUP BY
子句,则ORDER BY time DESC
必须出现在GROUP BY
子句之后。如果查询包含WHERE
子句且没有GROUP BY
子句,则ORDER BY time DESC
必须出现在WHERE
子句之后。
示例:
Return the newest points first
> SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' ORDER BY time DESC
name: h2o_feet
time water_level
---- -----------
2015-09-18T21:42:00Z 4.938
2015-09-18T21:36:00Z 5.066
[...]
2015-08-18T00:06:00Z 2.116
2015-08-18T00:00:00Z 2.064
Return the newest points first and include a GROUP BY time() clause
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:42:00Z' GROUP BY time(12m) ORDER BY time DESC
name: h2o_feet
time mean
---- ----
2015-08-18T00:36:00Z 4.6825
2015-08-18T00:24:00Z 4.80675
2015-08-18T00:12:00Z 4.950749999999999
2015-08-18T00:00:00Z 5.07625
LIMIT返回指定测量值的前N个点。
SELECT_clause [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] LIMIT
N指定从指定测量返回的点数,如果N大于测量中的点数,则InfluxDB将返回该序列中的所有点数。
注意:LIMIT
子句必须按照上述语法中列出的顺序出现。
示例
Limit the number of points returned
> SELECT "water_level","location" FROM "h2o_feet" LIMIT 3
name: h2o_feet
time water_level location
---- ----------- --------
2015-08-18T00:00:00Z 8.12 coyote_creek
2015-08-18T00:00:00Z 2.064 santa_monica
2015-08-18T00:06:00Z 8.005 coyote_creek
Limit the number points returned and include a GROUP BY clause
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:42:00Z' GROUP BY *,time(12m) LIMIT 2
name: h2o_feet
tags: location=coyote_creek
time mean
---- ----
2015-08-18T00:00:00Z 8.0625
2015-08-18T00:12:00Z 7.8245
name: h2o_feet
tags: location=santa_monica
time mean
---- ----
2015-08-18T00:00:00Z 2.09
2015-08-18T00:12:00Z 2.077
查询使用InfluxQL函数和GROUP BY
子句计算查询时间范围内每个标记和每个12分钟间隔的平均水位。LIMIT 2
请求两个最早的12分钟平均值(由时间戳确定)。
注意,在没有LIMIT 2
的情况下,查询将在每个序列中返回4个点;在查询的时间范围内,每12分钟间隔返回一个点。
SLIMIT
返回指定测量中序列中的每个点。
SELECT_clause [INTO_clause] FROM_clause [WHERE_clause] GROUP BY *[,time()] [ORDER_BY_clause] SLIMIT
N指定从指定测量返回的序列数。如果N大于度量中的序列数,InfluxDB将返回该度量中的所有序列。
有一个持续的问题需要SLIMIT查询包含GROUP BY *。请注意,SLIMIT子句必须按上述语法中列出的顺序出现。
示例
Limit the number of series returned
> SELECT "water_level" FROM "h2o_feet" GROUP BY * SLIMIT 1
name: h2o_feet
tags: location=coyote_creek
time water_level
---- -----
2015-08-18T00:00:00Z 8.12
2015-08-18T00:06:00Z 8.005
2015-08-18T00:12:00Z 7.887
[...]
2015-09-18T16:12:00Z 3.402
2015-09-18T16:18:00Z 3.314
2015-09-18T16:24:00Z 3.235
查询返回与h2o_feet
相关联的序列之一中的所有点。
Limit the number of series returned and include a GROUP BY time() clause
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:42:00Z' GROUP BY *,time(12m) SLIMIT 1
name: h2o_feet
tags: location=coyote_creek
time mean
---- ----
2015-08-18T00:00:00Z 8.0625
2015-08-18T00:12:00Z 7.8245
2015-08-18T00:24:00Z 7.5675
2015-08-18T00:36:00Z 7.303
查询使用InfluxQL函数和GROUP BY
子句中的时间间隔来计算查询时间范围内每12分钟间隔的平均water_level
。SLIMIT 1要求与h2o_feet
测量相关的单个系列。
注意,如果没有SLIMIT 1,查询将返回与h2o_feet
测量相关的两个系列的结果:location=coyote_creek
和location=santa_monica
。
LIMIT followed by SLIMIT returns the first points from series in the specified measurement.
SELECT_clause [INTO_clause] FROM_clause [WHERE_clause] GROUP BY *[,time()] [ORDER_BY_clause] LIMIT SLIMIT
N1指定每次测量返回的点数。如果N1大于测量中的点数,InfluxDB将返回该测量中的所有点数。
N2指定从指定测量返回的序列数。如果N2大于测量中的序列数,InfluxDB将返回该测量中的所有序列。
存在一个持续的问题,需要使用LIMIT
和SLIMIT
查询来包含GROUP BY *
。注意,LIMIT
和SLIMIT
子句必须按上述语法中列出的顺序出现。
示例
Limit the number of points and series returned
> SELECT "water_level" FROM "h2o_feet" GROUP BY * LIMIT 3 SLIMIT 1
name: h2o_feet
tags: location=coyote_creek
time water_level
---- -----------
2015-08-18T00:00:00Z 8.12
2015-08-18T00:06:00Z 8.005
2015-08-18T00:12:00Z 7.887
查询返回与测量h2o_feet
相关的序列之一中的三个最早点(由时间戳确定)。
Limit the number of points and series returned and include a GROUP BY time() clause
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:42:00Z' GROUP BY *,time(12m) LIMIT 2 SLIMIT 1
name: h2o_feet
tags: location=coyote_creek
time mean
---- ----
2015-08-18T00:00:00Z 8.0625
2015-08-18T00:12:00Z 7.8245
查询使用InfluxQL函数和GROUP BY
子句中的时间间隔来计算查询时间范围内每12分钟间隔的平均水位。LIMIT 2请求两个最早的12分钟平均值(由时间戳确定),SLIMIT 1请求与水英尺测量相关的单个序列。
请注意,如果没有LIMIT 2
SLIMIT 1
,查询将为与h2o_feet
测量相关的两个系列中的每一个返回四个点。
OFFSET
和SOFFSET
对返回的点和序列进行分页。
OFFSET
为查询结果中的N个点分页。
SELECT_clause [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] LIMIT_clause OFFSET [SLIMIT_clause]
N指定要分页的点数。OFFSET
子句需要一个LIMIT
子句。使用不带LIMIT
子句的OFFSET
子句可能会导致查询结果不一致。
注意:如果
WHERE
子句包含时间范围,并且OFFSET
子句将导致InfluxDB返回时间戳超出该时间范围的点,则InfluxDB不会返回任何结果。
示例
Paginate points
> SELECT "water_level","location" FROM "h2o_feet" LIMIT 3 OFFSET 3
name: h2o_feet
time water_level location
---- ----------- --------
2015-08-18T00:06:00Z 2.116 santa_monica
2015-08-18T00:12:00Z 7.887 coyote_creek
2015-08-18T00:12:00Z 2.028 santa_monica
查询返回water_level
测量值的第四、第五和第六个点。如果查询不包括OFFSET 3
,它将返回该度量的第一、第二和第三个点。
Paginate points and include several clauses
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:42:00Z' GROUP BY *,time(12m) ORDER BY time DESC LIMIT 2 OFFSET 2 SLIMIT 1
name: h2o_feet
tags: location=coyote_creek
time mean
---- ----
2015-08-18T00:12:00Z 7.8245
2015-08-18T00:00:00Z 8.0625
这个例子非常复杂,下面是逐条分解:
SELECT
子句指定一个InfluxQL
函数。FROM
子句指定单个度量。WHERE
子句指定查询的时间范围。GROUP BY
子句按所有标记(*
)将结果分组,并以12分钟为间隔。ORDER BY time DESC
子句返回时间戳降序结果。LIMIT 2
子句将返回的点数限制为2。OFFSET 2
子句从查询结果中排除前两个平均值。SLIMIT 1
子句将返回的序列数限制为1。
如果没有偏移量2,查询将返回查询结果的前两个平均值:
name: h2o_feet
tags: location=coyote_creek
time mean
---- ----
2015-08-18T00:36:00Z 7.303
2015-08-18T00:24:00Z 7.5675
SOFFSET
在查询结果中分页N个序列。
SELECT_clause [INTO_clause] FROM_clause [WHERE_clause] GROUP BY *[,time(time_interval)] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] SLIMIT_clause SOFFSET
N指定要分页的序列数。SOFFSET
子句需要SLIMIT
子句。在没有SLIMIT
子句的情况下使用SOFFSET
子句可能会导致查询结果不一致。有一个持续的问题需要SLIMIT
查询包含GROUP BY *
。
注意:如果
SOFFSET
子句分页超过序列总数,则InfluxDB不返回任何结果。
示例
Paginate series
> SELECT "water_level" FROM "h2o_feet" GROUP BY * SLIMIT 1 SOFFSET 1
name: h2o_feet
tags: location=santa_monica
time water_level
---- -----------
2015-08-18T00:00:00Z 2.064
2015-08-18T00:06:00Z 2.116
[...]
2015-09-18T21:36:00Z 5.066
2015-09-18T21:42:00Z 4.938
查询返回与water_level
测量和location=santa_monica
标记相关联的系列的数据。如果没有SOFFSET 1
,查询将返回与water_level
测量和location=coyote_creek
标记相关的系列数据。
Paginate series and include all clauses
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:42:00Z' GROUP BY *,time(12m) ORDER BY time DESC LIMIT 2 OFFSET 2 SLIMIT 1 SOFFSET 1
name: h2o_feet
tags: location=santa_monica
time mean
---- ----
2015-08-18T00:12:00Z 2.077
2015-08-18T00:00:00Z 2.09
这个例子非常复杂,下面是逐条分解:
SELECT
子句指定一个InfluxQL函数。FROM
子句指定单个度量。WHERE
子句指定查询的时间范围。GROUP BY
子句按所有标记(*
)将结果分组,并以12分钟为间隔。ORDER BY time DESC
子句返回时间戳降序结果。LIMIT 2
子句将返回的点数限制为2。OFFSET 2
子句从查询结果中排除前两个平均值。SLIMIT 1
子句将返回的序列数限制为1。SOFFSET 1
子句对返回的序列分页。
如果没有SOFFSET 1
,查询将返回不同系列的结果:
name: h2o_feet
tags: location=coyote_creek
time mean
---- ----
2015-08-18T00:12:00Z 7.8245
2015-08-18T00:00:00Z 8.0625
tz()
子句返回指定时区的UTC偏移量。
SELECT_clause [INTO_clause] FROM_clause [WHERE_clause] [GROUP_BY_clause] [ORDER_BY_clause] [LIMIT_clause] [OFFSET_clause] [SLIMIT_clause] [SOFFSET_clause] tz('')
默认情况下,InfluxDB以UTC格式存储并返回时间戳。tz()
子句包括UTC偏移量,或者,如果适用的话,查询返回时间戳的UTC夏时制时间(DST)偏移量。返回的时间戳必须采用RFC3339格式才能显示UTC偏移量或UTC DST。时区参数遵循Internet Assigned Numbers Authority
时区数据库中的TZ语法,需要单引号。
示例
Return the UTC offset for Chicago’s time zone
> SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:18:00Z' tz('America/Chicago')
name: h2o_feet
time water_level
---- -----------
2015-08-17T19:00:00-05:00 2.064
2015-08-17T19:06:00-05:00 2.116
2015-08-17T19:12:00-05:00 2.028
2015-08-17T19:18:00-05:00 2.126
查询结果包括时间戳中美洲/芝加哥时区的UTC偏移量(-05:00)。
对于大多数SELECT
语句,默认时间范围是在1677-09-21 00:12:43.145224194
和2262-04-11T23:47:16.854775806Z
UTC之间。对于带有GROUP BY time()
子句的SELECT
语句,默认时间范围是1677-09-21 00:12:43.145224194
UTC和now()
之间。以下各节详细介绍如何在SELECT
语句的WHERE
子句中指定可选时间范围。
使用日期时间字符串和纪元时间指定绝对时间。
SELECT_clause FROM_clause WHERE time ['' | '' | ] [AND ['' | '' | ] [...]]
支持的操作符
Operator | Meaning |
---|---|
= |
equal to |
<> |
not equal to |
!= |
not equal to |
> |
greater than |
>= |
greater than or equal to |
< |
less than |
<= |
less than or equal to |
目前,InfluxDB不支持在WHERE
子句中使用OR
连接绝对时间。有关更多信息,请参阅常见问题文档和GitHub问题。
rfc3339_date_time_string
'YYYY-MM-DDTHH:MM:SS.nnnnnnnnnZ'
.nnnnnnn
是可选的,如果不包括,则设置为.000000000
。RFC3339日期时间字符串需要单引号。
####rfc3339_like_date_time_string
'YYYY-MM-DD HH:MM:SS.nnnnnnnnn'
HH:MM:SS.nnnnnnnnn.nnnnnnnnn
是可选的,如果不包括,则设置为00:00:00.000000000
。类似于rfc339的日期-时间字符串需要单引号。
epoch_time
Epoch time是自1970年1月1日星期四00:00:00协调世界时(UTC)起经过的时间量。
默认情况下,InfluxDB假设所有纪元时间戳都以纳秒为单位。在epoch时间戳的末尾包含一个duration文本,以指示纳秒以外的精度。
####Basic arithmetic
所有时间戳格式都支持基本算法。用持续时间文字从时间戳中加上或减去时间。注意,InfluxQL需要一个介于+
或-
和duration文本之间的空白。
> SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= '2015-08-18T00:00:00.000000000Z' AND time <= '2015-08-18T00:12: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年8月18日00:00:00.000000000和2015年8月18日00:12:00之间的数据。第一个时间戳(.000000000
)中的纳秒规格是可选的。
请注意,RFC3339日期-时间字符串周围的单引号是必需的。
> SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= '2015-08-18' AND time <= '2015-08-18 00:12:00'
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年8月18日00:00:00到2015年8月18日00:12:00之间的数据。第一个日期时间字符串不包含时间;InfluxDB假定时间为00:00:00。
注意,rfc339类日期-时间字符串周围的单引号是必需的。
> SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= 1439856000000000000 AND time <= 1439856720000000000
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年8月18日00:00:00和2015年8月18日00:12:00之间的数据。默认情况下,InfluxDB假设纪元时间戳以纳秒为单位。
> SELECT "water_level" FROM "h2o_feet" WHERE "location" = 'santa_monica' AND time >= 1439856000s AND time <= 1439856720s
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年8月18日00:00:00和2015年8月18日00:12:00之间的数据。epoch时间戳末尾的s duration文字表示epoch时间戳以秒为单位。
> SELECT "water_level" FROM "h2o_feet" WHERE time > '2015-09-18T21:24:00Z' + 6m
name: h2o_feet
time water_level
---- -----------
2015-09-18T21:36:00Z 5.066
2015-09-18T21:42:00Z 4.938
查询返回时间戳为2015年9月18日21:24:00之后至少六分钟的数据。注意,+
和6m
之间的空白是必需的。
> SELECT "water_level" FROM "h2o_feet" WHERE time > 24043524m - 6m
name: h2o_feet
time water_level
---- -----------
2015-09-18T21:24:00Z 5.013
2015-09-18T21:30:00Z 5.01
2015-09-18T21:36:00Z 5.066
2015-09-18T21:42:00Z 4.938
查询返回带有时间戳的数据,时间戳至少在2015年9月18日21:24:00之前6分钟出现。注意-
和6m
之间的空白是必需的。
使用now()
查询具有相对于服务器当前时间戳的时间戳的数据。
SELECT_clause FROM_clause WHERE time now() [[ - | + ] ] [(AND|OR) now() [...]]
now()
是服务器在该服务器上执行查询时的Unix时间。-
或+
和持续时间文本之间的空白是必需的。
Operator | Meaning |
---|---|
= |
equal to |
<> |
not equal to |
!= |
not equal to |
> |
greater than |
>= |
greater than or equal to |
< |
less than |
<= |
less than or equal to |
u
or µ
microseconds ms
milliseconds s
seconds m
minutes h
hours d
days w
weeks
> SELECT "water_level" FROM "h2o_feet" WHERE time > now() - 1h
查询返回在发生在过去1小时内的时间戳的数据,-
和1h
之间的空白是必须的。
> SELECT "level description" FROM "h2o_feet" WHERE time > '2015-09-18T21:18:00Z' AND time < now() + 1000d
name: h2o_feet
time level description
---- -----------------
2015-09-18T21:24:00Z between 3 and 6 feet
2015-09-18T21:30:00Z between 3 and 6 feet
2015-09-18T21:36:00Z between 3 and 6 feet
2015-09-18T21:42:00Z between 3 and 6 feet
查询返回时间戳在2015年9月18日21:18:00到1000天(从现在起)之间的数据。+
和1000d
之间的空白是必须的。
OR
to select time multiple time intervalsInfluxDB不支持在WHERE
子句中使用OR
运算符指定多个时间间隔。
有关详细信息,请参见Frequently asked questions.
GROUP BY time()
子句查询now()
之后发生的数据大多数SELECT语句的默认时间范围为1677-09-21 00:12:43.145224194
和2262-04-11T23:47:16.854775806Z
UTC。对于带有GROUP BY time()
子句的SELECT
语句,默认时间范围是1677-09-21 00:12:43.145224194
UTC和now()
之间。
要使用now()
之后出现的时间戳查询数据,带有GROUP BY time()
子句的SELECT
语句必须在WHERE
子句中提供另一个上界。
Use the CLI to write a point to the NOAA_water_database
that occurs after now()
:
> INSERT h2o_feet,location=santa_monica water_level=3.1 1587074400000000000
运行GROUP BY time()
查询,该查询覆盖2015-09-18T21:30:00Z
到now()
之间的带有时间戳的数据:
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location"='santa_monica' AND time >= '2015-09-18T21:30:00Z' GROUP BY time(12m) fill(none)
name: h2o_feet
time mean
---- ----
2015-09-18T21:24:00Z 5.01
2015-09-18T21:36:00Z 5.002
运行GROUP BY time()
查询,该查询涵盖2015-09-18T21:30:00Z
到now()
的180周后的时间戳数据:
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location"='santa_monica' AND time >= '2015-09-18T21:30:00Z' AND time <= now() + 180w GROUP BY time(12m) fill(none)
name: h2o_feet
time mean
---- ----
2015-09-18T21:24:00Z 5.01
2015-09-18T21:36:00Z 5.002
2020-04-16T22:00:00Z 3.1
请注意,WHERE
子句必须提供一个替代上限,以覆盖默认now()
上限。以下查询仅将下限重置为now()
,以便查询的时间范围介于now()
和now()
之间:
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location"='santa_monica' AND time >= now() GROUP BY time(12m) fill(none)
默认情况下,CLI以纳米纪元格式返回时间戳,使用precision
命令指定替代格式。默认情况下,InfluxDB API以RFC3339格式返回时间戳,使用epoch
查询字符串指定替代格式。
InfluxQL supports using regular expressions when specifying:
SELECT
clauseFROM
clauseWHERE
clause.GROUP BY
clause目前,InfluxQL不支持使用正则表达式来匹配WHERE
子句、数据库和保留策略中的非字符串字段值。
注意:正则表达式比较比精确字符串比较在计算上更密集;使用正则表达式的查询的性能不如不使用正则表达式的查询。
SELECT // FROM // WHERE [ // | //] GROUP BY //
正则表达式由/
字符包围,并使用Golang的正则表达式语法。
=~
matches against !~
doesn’t match against
> SELECT /l/ FROM "h2o_feet" LIMIT 1
name: h2o_feet
time level description location water_level
---- ----------------- -------- -----------
2015-08-18T00:00:00Z between 6 and 9 feet coyote_creek 8.12
查询将选择包含l
的所有字段键和标记键。请注意,SELECT
子句中的正则表达式必须至少与一个字段键匹配,才能返回与正则表达式匹配的标记键的结果。
目前,SELECT
子句中没有语法来区分字段键的正则表达式和标记键的正则表达式。语法/
不受支持。
> SELECT MEAN("degrees") FROM /temperature/
name: average_temperature
time mean
---- ----
1970-01-01T00:00:00Z 79.98472932232272
name: h2o_temperature
time mean
---- ----
1970-01-01T00:00:00Z 64.98872722506226
查询使用InfluxQL函数计算NOAA_water_database
中包含单词temperature
的每个测量值的平均degrees
。
> SELECT MEAN(water_level) FROM "h2o_feet" WHERE "location" =~ /[m]/ AND "water_level" > 3
name: h2o_feet
time mean
---- ----
1970-01-01T00:00:00Z 4.47155532049926
查询使用InfluxQL函数计算平均water_level
,其中location
的标记值包含m
且water_level
大于3。
> SELECT * FROM "h2o_feet" WHERE "location" !~ /./
查询将从位置标记没有值的h2o_feet
测量中选择所有数据。NOAA_water_database
的每个数据点都有一个位置标记值。
可以在不使用正则表达式的情况下执行相同的查询。有关详细信息,请参阅常见问题文档。
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location" = 'santa_monica' AND "level description" =~ /between/
name: h2o_feet
time mean
---- ----
1970-01-01T00:00:00Z 4.47155532049926
查询使用InfluxQL函数计算所有数据的平均water_level
,其中level description
的字段值包括between
单词。
> SELECT FIRST("index") FROM "h2o_quality" GROUP BY /l/
name: h2o_quality
tags: location=coyote_creek
time first
---- -----
2015-08-18T00:00:00Z 41
name: h2o_quality
tags: location=santa_monica
time first
---- -----
2015-08-18T00:00:00Z 99
查询使用InfluxQL函数为每个标记选择index
的第一个值,该标记在其标记键中包含字母l
。
Field values can be floats, integers, strings, or booleans. The ::
syntax allows users to specify the field’s type in a query.
Note: Generally, it is not necessary to specify the field value type in the
SELECT
clause. In most cases, InfluxDB rejects any writes that attempt to write a field value to a field that previously accepted field values of a different type.
It is possible for field value types to differ across shard groups. In these cases, it may be necessary to specify the field value type in theSELECT
clause. Please see the Frequently Asked Questions document for more information on how InfluxDB handles field value type discrepancies.
SELECT_clause :: FROM_clause
type
can be float
, integer
, string
, or boolean
. In most cases, InfluxDB returns no data if the field_key
does not store data of the specified type
. See Cast Operations for more information.
> SELECT "water_level"::float FROM "h2o_feet" LIMIT 4
name: h2o_feet
--------------
time water_level
2015-08-18T00:00:00Z 8.12
2015-08-18T00:00:00Z 2.064
2015-08-18T00:06:00Z 8.005
2015-08-18T00:06:00Z 2.116
The query returns values of the water_leve
The query returns values of the water_level
field key that are floats.
The ::
syntax allows users to perform basic cast operations in queries. Currently, InfluxDB supports casting field values from integers to floats or from floats to integers.
SELECT_clause <field_key>::<type> FROM_clause
type
can be float
or integer
.
InfluxDB returns no data if the query attempts to cast an integer or float to a string or boolean.
> SELECT "water_level"::integer FROM "h2o_feet" LIMIT 4
name: h2o_feet
--------------
time water_level
2015-08-18T00:00:00Z 8
2015-08-18T00:00:00Z 2
2015-08-18T00:06:00Z 8
2015-08-18T00:06:00Z 2
The query returns the integer form of water_level
’s float field values.
> SELECT "water_level"::string FROM "h2o_feet" LIMIT 4
>
The query returns no data as casting a float field value to a string is not yet supported.
In InfluxDB, queries merge series automatically.
The h2o_feet
measurement in the NOAA_water_database
is part of two series. The first series is made up of the h2o_feet
measurement and the location = coyote_creek
tag. The second series is made of up the h2o_feet
measurement and the location = santa_monica
tag.
The following query automatically merges those two series when it calculates the average water_level
:
> SELECT MEAN("water_level") FROM "h2o_feet"
name: h2o_feet
--------------
time mean
1970-01-01T00:00:00Z 4.442107025822521
If you want the average water_level
for the first series only, specify the relevant tag in the WHERE
clause:
> SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location" = 'coyote_creek'
name: h2o_feet
--------------
time mean
1970-01-01T00:00:00Z 5.359342451341401
If you want the average water_level
for each individual series, include a GROUP BY
clause:
> SELECT MEAN("water_level") FROM "h2o_feet" GROUP BY "location"
name: h2o_feet
tags: location=coyote_creek
time mean
---- ----
1970-01-01T00:00:00Z 5.359342451341401
name: h2o_feet
tags: location=santa_monica
time mean
---- ----
1970-01-01T00:00:00Z 3.530863470081006
Separate multiple SELECT
statements in a query with a semicolon (;
).
Example 1: CLIExample 2: InfluxDB API
In the InfluxDB CLI:
> SELECT MEAN("water_level") FROM "h2o_feet"; SELECT "water_level" FROM "h2o_feet" LIMIT 2
name: h2o_feet
time mean
---- ----
1970-01-01T00:00:00Z 4.442107025822522
name: h2o_feet
time water_level
---- -----------
2015-08-18T00:00:00Z 8.12
2015-08-18T00:00:00Z 2.064
A subquery is a query that is nested in the FROM
clause of another query. Use a subquery to apply a query as a condition in the enclosing query. Subqueries offer functionality similar to nested functions and SQL HAVING
clauses.
SELECT_clause FROM ( SELECT_statement ) [...]
InfluxDB performs the subquery first and the main query second.
The main query surrounds the subquery and requires at least the SELECT
clause and the FROM
clause. The main query supports all clauses listed in this document.
The subquery appears in the main query’s FROM
clause, and it requires surrounding parentheses. The subquery supports all clauses listed in this document.
InfluxQL supports multiple nested subqueries per main query. Sample syntax for multiple subqueries:
SELECT_clause FROM ( SELECT_clause FROM ( SELECT_statement ) [...] ) [...]
SUM()
of several MAX()
values> SELECT SUM("max") FROM (SELECT MAX("water_level") FROM "h2o_feet" GROUP BY "location")
name: h2o_feet
time sum
---- ---
1970-01-01T00:00:00Z 17.169
The query returns the sum of the maximum water_level
values across every tag value of location
.
InfluxDB first performs the subquery; it calculates the maximum value of water_level
for each tag value of location
:
> SELECT MAX("water_level") FROM "h2o_feet" GROUP BY "location"
name: h2o_feet
tags: location=coyote_creek
time max
---- ---
2015-08-29T07:24:00Z 9.964
name: h2o_feet
tags: location=santa_monica
time max
---- ---
2015-08-29T03:54:00Z 7.205
Next, InfluxDB performs the main query and calculates the sum of those maximum values: 9.964
+ 7.205
= 17.169
. Notice that the main query specifies max
, not water_level
, as the field key in the SUM()
function.
MEAN()
difference between two fields> SELECT MEAN("difference") FROM (SELECT "cats" - "dogs" AS "difference" FROM "pet_daycare")
name: pet_daycare
time mean
---- ----
1970-01-01T00:00:00Z 1.75
The query returns the average of the differences between the number of cats
and dogs
in the pet_daycare
measurement.
InfluxDB first performs the subquery. The subquery calculates the difference between the values in the cats
field and the values in the dogs
field, and it names the output column difference
:
> SELECT "cats" - "dogs" AS "difference" FROM "pet_daycare"
name: pet_daycare
time difference
---- ----------
2017-01-20T00:55:56Z -1
2017-01-21T00:55:56Z -49
2017-01-22T00:55:56Z 66
2017-01-23T00:55:56Z -9
Next, InfluxDB performs the main query and calculates the average of those differences. Notice that the main query specifies difference
as the field key in the MEAN()
function.
MEAN()
values and place a condition on those mean values> SELECT "all_the_means" FROM (SELECT MEAN("water_level") AS "all_the_means" FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z' GROUP BY time(12m) ) WHERE "all_the_means" > 5
name: h2o_feet
time all_the_means
---- -------------
2015-08-18T00:00:00Z 5.07625
The query returns all mean values of the water_level
field that are greater than five.
InfluxDB first performs the subquery. The subquery calculates MEAN()
values of water_level
from 2015-08-18T00:00:00Z
through 2015-08-18T00:30:00Z
and groups the results into 12-minute intervals. It also names the output column all_the_means
:
> SELECT MEAN("water_level") AS "all_the_means" FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z' GROUP BY time(12m)
name: h2o_feet
time all_the_means
---- -------------
2015-08-18T00:00:00Z 5.07625
2015-08-18T00:12:00Z 4.950749999999999
2015-08-18T00:24:00Z 4.80675
Next, InfluxDB performs the main query and returns only those mean values that are greater than five. Notice that the main query specifies all_the_means
as the field key in the SELECT
clause.
SUM()
of several DERIVATIVE()
values> SELECT SUM("water_level_derivative") AS "sum_derivative" FROM (SELECT DERIVATIVE(MEAN("water_level")) AS "water_level_derivative" FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z' GROUP BY time(12m),"location") GROUP BY "location"
name: h2o_feet
tags: location=coyote_creek
time sum_derivative
---- --------------
1970-01-01T00:00:00Z -0.4950000000000001
name: h2o_feet
tags: location=santa_monica
time sum_derivative
---- --------------
1970-01-01T00:00:00Z -0.043999999999999595
The query returns the sum of the derivative of average water_level
values for each tag value of location
.
InfluxDB first performs the subquery. The subquery calculates the derivative of average water_level
values taken at 12-minute intervals. It performs that calculation for each tag value of location
and names the output column water_level_derivative
:
> SELECT DERIVATIVE(MEAN("water_level")) AS "water_level_derivative" FROM "h2o_feet" WHERE time >= '2015-08-18T00:00:00Z' AND time <= '2015-08-18T00:30:00Z' GROUP BY time(12m),"location"
name: h2o_feet
tags: location=coyote_creek
time water_level_derivative
---- ----------------------
2015-08-18T00:12:00Z -0.23800000000000043
2015-08-18T00:24:00Z -0.2569999999999997
name: h2o_feet
tags: location=santa_monica
time water_level_derivative
---- ----------------------
2015-08-18T00:12:00Z -0.0129999999999999
2015-08-18T00:24:00Z -0.030999999999999694
Next, InfluxDB performs the main query and calculates the sum of the water_level_derivative
values for each tag value of location
. Notice that the main query specifies water_level_derivative
, not water_level
or derivative
, as the field key in the SUM()
function.
InfluxQL supports multiple nested subqueries per main query:
SELECT_clause FROM ( SELECT_clause FROM ( SELECT_statement ) [...] ) [...]
------------------ ----------------
Subquery 1 Subquery 2
InfluxQL does not support multiple SELECT
statements per subquery:
SELECT_clause FROM (SELECT_statement; SELECT_statement) [...]
The system returns a parsing error if a subquery includes multiple SELECT
statements.