子查询
子查询是嵌套在另一个查询的FROM
子句中的查询。使用子查询将查询作为条件应用在另一个查询中。子查询提供类似嵌套函数和SQL HAVING
子句的功能。
语法
SELECT_clause FROM ( SELECT_statement ) [...]
语法描述
TSDB For InfluxDB®首先执行子查询,然后执行主查询。
主查询包含着子查询,至少需要SELECT
子句和FROM
子句。主查询支持本文档中列出的所有子句。
子查询在主查询的FROM
子句中,需要用括号将子查询括起来。子查询支持本文档中列出的所有子句。
InfluxQL支持在主查询中有多个嵌套的子查询,示例语法如下:
SELECT_clause FROM ( SELECT_clause FROM ( SELECT_statement ) [...] ) [...]
示例
计算多个MAX()
值的SUM()
> 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
该查询返回每个location
中water_level
的最大值的总和。
TSDB For InfluxDB®首先执行子查询,计算每个location
的water_level
的最大值:
> 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
然后,TSDB For InfluxDB®执行主查询,计算这些最大值的总和:9.964 + 7.205 = 17.169。请注意,该主查询指定max
(而不是water_level
)作为SUM()
函数中的field key。
计算两个field的差值的MEAN()
> SELECT MEAN("difference") FROM (SELECT "cats" - "dogs" AS "difference" FROM "pet_daycare")
name: pet_daycare
time mean
---- ----
1970-01-01T00:00:00Z 1.75
该查询返回measurement pet_daycare
中cats
数量和dogs
数量的差异的平均值。
TSDB For InfluxDB®首先执行子查询,计算field cats
中的值和field dogs
中的值的差异,并将输出列命名为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
然后,TSDB For InfluxDB®执行主查询,计算这些差值的平均值。请注意,该主查询指定difference
作为MEAN()
函数中的field key。
计算多个MEAN()
值并在这些值上加上条件
> 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
该查询返回water_level
的所有大于5的平均值。
TSDB For InfluxDB®首先执行子查询,计算从2015-08-18T00:00:00Z
到2015-08-18T00:30:00Z
water_level
的平均值,并将结果按12分钟的时间间隔进行分组,同时将输出列命名为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
然后,TSDB For InfluxDB®执行主查询,只返回那些大于5的平均值。请注意,该主查询指定all_the_means
作为SELECT子句中的field key。
计算多个DERIVATIVE()
值的SUM()
> 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
该查询返回每个location
中water_level
的平均值的导数之和。
TSDB For InfluxDB®首先执行子查询,计算以12分钟为间隔获取的water_level
的平均值的导数,它对每个location
都进行了计算,并将输出列命名为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
然后,TSDB For InfluxDB®执行主查询,计算每个location
的water_level_derivative
的总和。请注意,该主查询指定water_level_derivative
(而不是water_level
或derivative
)作为SUM()
函数中的field key。
子查询的常见问题
在子查询中有多个SELECT
语句
InfluxQL支持在主查询中有多个嵌套的子查询:
SELECT_clause FROM ( SELECT_clause FROM ( SELECT_statement ) [...] ) [...]
------------------ ----------------
Subquery 1 Subquery 2
InfluxQL不支持在子查询中有多个SELECT
语句:
SELECT_clause FROM (SELECT_statement; SELECT_statement) [...]
如果在子查询中有多个SELECT
语句,那么系统会返回解析错误。
InfluxDB® is a trademark registered by InfluxData, which is not affiliated with, and does not endorse, TSDB for InfluxDB®.