You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

InfluxDB分组查询如何获取每日最后数据的对应时间?

Hey there! I see the issue you're running into—your grouped query is returning the start time of each time window instead of the actual timestamp of the last record in that window, even though the last(val1) and last(val2) values are correct. Let's fix that!

The root cause here is that when you use GROUP BY time(), InfluxDB defaults to returning the start timestamp of each grouped time interval (the adjusted one with your -60m-1s offset) rather than the timestamp associated with the last() values you're pulling. To get the actual time of the last record in each group, you just need to add last(time) to your SELECT clause.

Here's your modified query:

SELECT last(time) AS last_record_time, last(val1), last(val2) 
FROM powerandlevel 
WHERE id='$id' 
  AND time > 1606761000000000000 + 1d 
  AND time < 1609093800000000000 + 1d 
GROUP BY time(1d, -60m-1s)

What this does:

  • last(time) grabs the actual timestamp of the very last data point in each 1-day window (with your offset applied).
  • Using AS last_record_time gives this column a clear, readable name so you don't get confused with the default window start time.

When you run this query, your results will now show the real timestamp of the last entry (like 2020-12-26T22:59:59.677Z for the group that previously showed 2020-12-25T22:59:59Z), alongside the correct last(val1) and last(val2) values you already had.

This works perfectly in InfluxDB 1.x (using InfluxQL) which is what your queries are written in. If you ever move to InfluxDB 2.x with Flux, the approach is similar but uses different syntax, but this solution should solve your current problem right away.

内容的提问来源于stack exchange,提问作者Md Enayat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:35:05