go - 如何为每个组选择最接近给定时间的数据
问题描述
我正在使用 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 或其他方式使用分组来完成这项任务吗?
解决方案
问:我想要select
最接近小时边界的点数据。有没有一种方法可以做到这一点,而不必每天查询 24 次?对此有什么帮助吗group by time
?
A:
对此有什么帮助吗
group by time
?
不幸的是,该group by time
函数对您没有太大帮助,因为它要求查询具有聚合函数。该函数的作用是通过使用诸如等函数将组合行的值制成表格,group by time
将区间内的所有数据分组为一条记录。aggregation
sum
mean
有没有一种方法可以做到这一点,而不必每天查询 24 次?
据我所知,我认为influxdb 1.5
没有任何方法可以为此任务构建单行查询。也许里面有东西1.6
,我不确定。没试过。
目前我认为您今天最好的解决方案是构建一个使用time filter
,order by
和limit
功能的查询,例如
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
推荐阅读
- python - 如何在 django 中将值从一个模型存储到另一个模型?
- eclipse - Eclipse 隐藏项目包以便在包资源管理器中轻松导航
- java - JavaFX:使用 Permanin 扭曲创建板网格
- image-processing - Maya 渲染图像、相机矩阵
- java - 根据扫描仪计算行数/最大字符数
- python-2.7 - 如何在python中对仅产生英语字母的两个字符串进行异或?
- xslt-1.0 - XSLT 1.0 转换使用变量删除重复项
- c# - Argumentoutofrangeexception 是不是除外?C# 统一
- javascript - 切换下一部分
- azure - 在 https://dev.botframework.com/ 上注册 Azure 机器人