首页 > 解决方案 > 如何为每个组选择最接近给定时间的数据

问题描述

我正在使用 InfluxDB 1.4,这是我的任务

1) find the closet value for each IDs. 
2) Do 1) for every hour

例如,

select id, value, time from myTable where time = '2018-08-14T00:00:00Z' group by id;
select id, value, time from myTable where time = '2018-08-14T01:00:00Z' group by id;
....
select id, value, time from myTable where time = '2018-08-14T23:00:00Z' group by id;

然后,一些 id 在每个点都有价值,而另一些则没有。在这种情况下,我想获得最接近给定时间“2018-08-14T14:00:00Z”的行,例如“2018-08-14T14:00:01Z”或“2018-08-14T13:59”: 59Z'

而且我不想每小时查询 24 次。我可以按时间、ID 或其他方式使用分组来完成这项任务吗?

标签: goinfluxdb

解决方案


问:我想要select最接近小时边界的点数据。有没有一种方法可以做到这一点,而不必每天查询 24 次?对此有什么帮助吗group by time

A:

对此有什么帮助吗group by time

不幸的是,该group by time函数对您没有太大帮助,因为它要求查询具有聚合函数。该函数的作用是通过使用诸如等函数将组合行的值制成表格,group by time将区间内的所有数据分组为一条记录。aggregationsummean

有没有一种方法可以做到这一点,而不必每天查询 24 次?

据我所知,我认为influxdb 1.5没有任何方法可以为此任务构建单行查询。也许里面有东西1.6,我不确定。没试过。

目前我认为您今天最好的解决方案是构建一个使用time filter,order bylimit功能的查询,例如

select * from uv where time >= '2018-08-18T14:00:00Z' and time < '2018-08-18T15:00:00Z' order by desc limit 1;

上面的查询意味着你是selecting下午 2 点到 3 点之间的所有点,然后按降序排列它们,但只返回第一行,这就是你想要的。

如果由于某种原因,您只能针对特定日期的每小时数据向 influxdb 发出 1 个 HTTP 请求。;您可以使用分隔符将 24 个查询捆绑到一个大查询中,并在 1 个事务中检索数据。例如

select * from uv where time >= '2018-08-18T14:00:00Z' and time < '2018-08-18T15:00:00Z' order by desc limit 1; select * from uv where time >= '2018-08-18T15:00:00Z' and time < '2018-08-18T16:00:00Z' order by desc limit 1; select * from uv where time >= '2018-08-18T16:00:00Z' and time < '2018-08-18T17:00:00Z' order by desc limit 1;

输出:

name: uv
time                tag1 id         value
----                -------- --         -----
1534603500000000000 apple  uv 2
1534607100000000000 apple  uv 1
1534610700000000000 apple  uv 3.1

推荐阅读