使用 TimescaleDB 计算具有双整数时间间隔的平均值

Calculating average with biginteger time intervals using TimescaleDB

我有一个包含以下字段的架构:

       Name of row          |   Type    
  --------------------------+--------
  name                      | string
  value1                    | numeric
  timestamp                 | bigint

这些行包含具有名称、数值和以纳秒为单位存储 unix 时间戳的 bigint 值的条目。使用 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

这 returns 值正确,但未填充空格。如果我将查询修改为

time_bucket_gapfill('5 min', 
                    date_trunc('quarter', 
                               to_timestamp(timestamp/1000000000), 
                               to_timestamp(1599100000), 
                               to_timestamp(1599200000))

我会正确获取第一个条目,然后每 5 分钟清空一行。我怎样才能让它发挥作用?谢谢!

Here 是一个数据库 fiddle,但它不起作用,因为它不支持 TimeScaleDB。上面的查询 returns 以下:

  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

您应该在 time_bucket_gapfill 中使用与 table 中的数据类型匹配的数据类型。以下查询应该可以找到您要查找的内容:

SELECT
  COALESCE(count(*), 0),
  COALESCE(SUM(value1), 0),
  time_bucket_gapfill(300E9::BIGINT, timestamp) AS bucket
FROM
  t
WHERE
  name = 'example'
  AND timestamp >= 1599100000000000000
  AND timestamp < 1599200000000000000
GROUP BY
  bucket;

我已经在 Sven 的回答的基础上解决了这个问题。它首先使用他的函数来填补空白,然后调用 date_trunc 消除多余的行。

WITH gapfill AS (
    SELECT 
        COALESCE(count(*), 0) as count, 
        COALESCE(SUM(value1), 0) as sum,
        time_bucket_gapfill(300E9::BIGINT, timestamp) as bucket
    FROM
        playground
    WHERE
        name = 'test'
        AND timestamp >= 1599100000000000000
        AND timestamp < 1599300000000000000
    GROUP BY
        bucket
)
SELECT
    SUM(count),
    SUM(sum),
    date_trunc('quarter', to_timestamp(bucket/1000000000)) as truncated
FROM 
    gapfill
GROUP BY truncated
ORDER BY truncated ASC