子查詢
子查詢是嵌套在另一個查詢的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?.