如何select每组最接近给定时间的数据

How to select the closest data to the given time for each group

我正在使用 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 或其他方式对群组执行此任务吗?

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

A:

Will group by time be any help on this?

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

Is there a way I can do this without having to query 24 times for each day?

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

目前,我认为您今天最好的解决方案是构建一个使用 time filterorder 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点之间的所有点,然后按降序排列但是只有return第一行,这就是你想要的。

如果出于某种原因您只能对特定日期的每小时数据向 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