【发布时间】:2020-10-14 11:10:25
【问题描述】:
我有一个包含以下字段的架构:
Name of row | Type
--------------------------+--------
name | string
value1 | numeric
timestamp | bigint
这些行包含具有名称、数值和 bigint 值的条目,以纳秒为单位存储 unix 时间戳。使用 TimescaleDB,我想使用 time_buckets_gapfill 来检索数据。鉴于时间戳存储在 bigint 中,这非常麻烦。
我想获取以下时间间隔的汇总数据:5 分钟、小时、日、周、月、季度、年。我已经设法使用普通的time_buckets 使其工作,但现在我也想填补空白。我现在使用以下查询:
SELECT COALESCE(COUNT(*), 0), COALESCE(SUM(value1), 0), time_bucket_gapfill('5 min', date_trunc('quarter', to_timestamp(timestamp/1000000000)), to_timestamp(1599100000), to_timestamp(1599300000)) AS bucket
FROM playground
WHERE name = 'test' AND timestamp >= 1599100000000000000 AND timestamp <= 1599300000000000000
GROUP BY bucket
ORDER BY bucket ASC
这会正确返回值,但不会填充空格。如果我将查询修改为
time_bucket_gapfill('5 min',
date_trunc('quarter',
to_timestamp(timestamp/1000000000),
to_timestamp(1599100000),
to_timestamp(1599200000))
我会正确获取第一个条目,然后每 5 分钟清空一次。我怎样才能让它工作?谢谢!
Here 是一个数据库小提琴,但它不工作,因为它不支持 TimeScaleDB。上面的查询返回以下内容:
coalesce | coalesce | avg_val
------------------------+-------------------------
3 | 300 | 2020-07-01 00:00:00+00
0 | 0 | 2020-09-03 02:25:00+00
0 | 0 | 2020-09-03 02:30:00+00
【问题讨论】:
-
请编辑您的问题并添加示例数据和所需的输出
标签: sql postgresql aggregate timescaledb