【发布时间】:2022-11-07 16:51:25
【问题描述】:
我在使用 Timescaledb 的表中有时间序列数据。
数据如下:
time locationid parameterid unitid value
2022-04-18T10:00:00.000Z "1" "1" "2" 2.2
2022-04-18T10:00:00.000Z "2" "1" "2" 3.0
2022-04-18T09:00:00.000Z "1" "1" "2" 1.2
2022-04-18T09:00:00.000Z "2" "1" "2" 4.0
2022-04-18T08:00:00.000Z "1" "1" "2" 2.6
2022-04-18T08:00:00.000Z "2" "1" "2" 3.1
2022-04-18T07:00:00.000Z "1" "1" "2" 2.1
2022-04-18T07:00:00.000Z "2" "1" "2" 2.7
我有 1000 行包含时间序列 IOT 数据的行,我正在使用 HighCharts 将这些数据放入图表中。
我的问题是,有没有办法限制我的结果中返回的项目数量,但不是经典限制。我想始终返回 256 个数据组。因此,如果我有 2,560 行,我的查询将按/日期 trunc / time_bucket 每 100 行分组,但如果我有 512 行,我的查询只会每 2 行分组一次,因此无论如何我总是返回 256。
我目前的查询:
SELECT time_bucket('4 hours', time) as "t"
,locationid, avg(timestamp) as "x", avg(value) as "y"
FROM probe_data
WHERE locationid = '${q.locationid}'and parameterid = '${q.parameterid}'
and time > '${q.startDate}' and time < `${q.endDate}`
GROUP BY "t", locationid
ORDER BY "t" DESC;
似乎我应该能够使用我的最小日期和最大日期来计算可能返回的数量,然后除以 256?这是最好的方法吗?
【问题讨论】:
-
时间都已经等距了吗?
-
是的,每个 locationid 的每次读数相隔 1 小时。
-
@gsmith1515 这是一个很好的问题(我来自 TimescaleDB)。如果我将它添加到我们的论坛以便其他用户可以找到它,我希望你没问题......如果你想要信用并且更喜欢自己写它,尽管让我知道!
-
@greenweeds 加油!
-
花了我一段时间,但我现在做到了,谢谢!
标签: sql group-by timescaledb