使用姿势
SELECT <function>(<field_key>) FROM_clause WHERE <time_range> GROUP BY time(<time_interval>),[tag_key] [fill(<fill_option>)]
group time(interval)会对查询结果按照interval进行聚合,例如,time(5m),interval=5m, 则会将数据每隔5m进行聚合
示例说明
样例数据
> 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
此时按照interval=12m 对数据进行聚合,查询语句以及查询结果如下:
> 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
InfluxDB在计算group by时,会用到两个时间区间,一个是根据interval形成的预设区间,一个是根据where语句中的time_range形成的区间,在上述例子中time_range为[2015-08-18T00:00:00Z,2015-08-18T00:30:00Z),详细说明如下所示
预设区间 | where time_range | 数据 | 返回时间戳 |
---|---|---|---|
time>=2015-08-18T00:00:00Z and time<2015-08-18T00:12:00Z | time>=2015-08-18T00:00:00Z and time<2015-08-18T00:12:00Z | 8.12,8.005 | 2015-08-18T00:00:00Z |
time>=2015-08-18T00:12:00Z and time<2015-08-18T00:24:00Z | time>=2015-08-18T00:12:00Z and time<2015-08-18T00:24:00Z | 7.887,7.762 | 2015-08-18T00:12:00Z |
time>=2015-08-18T00:24:00Z and time<2015-08-18T00:36:00Z | time>=2015-08-18T00:24:00Z and time<2015-08-18T00:30:00Z | 7.635,7.5 | 2015-08-18T00:24:00Z |
异常情况说明
有的时候使用group by time(),会发现查询结果的时间与where条件中的time_range冲突,这是因为InfluxDB在返回时间时是以预设区间的时间为主。详细说明如下
样本数据
> 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
查询语句以及查询结果:
> 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 <----- 时间戳超出where条件中的起始时间
2015-08-18T00:12:00Z 1
预设区间 | where time_range | 数据 | 返回时间戳 |
---|---|---|---|
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 |
time>=2015-08-18T00:12:00Z and time<2015-08-18T00:24:00Z | time>=2015-08-18T00:12:00Z and time<2015-08-18T00:18:00Z | 7.887 | 2015-08-18T00:12:00Z |
数据需要同时在预设区间和time_range在能满足查询条件,8.12这条数据在预设区间中,但是不在time_range中,所以不满足条件,只有8.005这条数据满足
如果要避免这种异常情况,需要group by time()的高级用法
高级用法
使用姿势
SELECT <function>(<field_key>) FROM_clause WHERE <time_range> GROUP BY time(<time_interval>,<offset_interval>),[tag_key] [fill(<fill_option>)]
与基本用法的唯一区别在于多了一个offset_interval的参数,这个参数表示在设置预设区间时需要偏移的时间,可以为正数或负数
若time_range为[2019-03-08T16:00:00Z, 2019-03-08T16:30:00Z)
###group by time(10m)
预设区间 | where time_range | 返回时间戳 |
---|---|---|
time>=2019-03-08T16:00:00Z and time<2019-03-08T16:10:00Z | time>=2019-03-08T16:00:00Z and time<2019-03-08T16:10:00Z | 2019-03-08T16:00:00Z |
time>=2019-03-08T16:10:00Z and time<2019-03-08T16:20:00Z | 与预设区间相同 | 2019-03-08T16:10:00Z |
time>=2019-03-08T16:20:00Z and time<2019-03-08T16:30:00Z | 与预设区间相同 | 2019-03-08T16:20:00Z |
###group by time(10m,-5m)
|预设区间|where time_range|返回时间戳
|—|—|—|
|time>=2019-03-08T15:55:00Z and time<2019-03-08T16:05:00Z|time>=2019-03-08T16:00:00Z and time<2019-03-08T16:10:00Z|2019-03-08T15:55:00Z
|time>=2019-03-08T16:05:00Z and time<2019-03-08T16:15:00Z|time>=2019-03-08T16:10:00Z and time<2019-03-08T16:20:00Z|2019-03-08T16:05:00Z
|time>=2019-03-08T16:15:00Z and time<2019-03-08T16:25:00Z|time>=2019-03-08T16:20:00Z and time<2019-03-08T16:30:00Z|2019-03-08T16:15:00Z
|time>=2019-03-08T16:25:00Z and time<2019-03-08T16:35:00Z||2019-03-08T16:25:00Z
group by time(1d)
InfluxDB默认是使用UTC时区,所以在进行计算group by time(1d),对于CST时区,数据会偏移8个小时,所以在聚合1d数据时,需要使用time(1d, -8h)