如何使用TimescaleDB按月进行时间分桶

4
截至目前,我知道有一个开放功能请求来添加月份/年份到time_bucket函数中。
我的问题是,现在实现这个的最佳方法是什么。 这个问题提到了date_trunc。
以下是两种方法:

timescaledb的time_bucket

SELECT time_bucket('1 week', timestamp) AS "one_week", 
       count(*) AS "count", 
       first(value, timestamp) AS "first", 
       last(value, timestamp) AS "last" 
FROM "event" "event" 
WHERE event."signalId" = $1 
GROUP BY one_week 
ORDER BY one_week DESC

postgres date_trunc

SELECT date_trunc('month', timestamp) AS "one_month", 
       count(*) AS "count", 
       first(value, timestamp) AS "first", 
       last(value, timestamp) AS "last" 
FROM "event" "event" 
WHERE event."signalId" = $1 
GROUP BY one_month 
ORDER BY one_month DESC

这两个都能正常工作(虽然我没有进行任何性能测试)。

我想要实现:

  • (a)填补间隙(例如timescaledb的time_bucket_gapfill),以及
  • (b)向前填充最后一个值(locf)

如何最好地实现这一点?

谢谢!


我遇到了同样的问题,很遗憾看到Timescale没有回应社区的请求。你是如何解决这个问题的? - Rob Angelier
1个回答

2
看起来他们计划在未来支持此功能,但尚未包含在任何里程碑规划中,请查看https://github.com/timescale/timescaledb/issues/414
目前,我使用time_bucket('1 day', timestamp)作为解决方法,或将其包含在CTE / with的一部分中,在其中调用date_trunc('month', time_bucketed_day_column)。这样,timescaledb的gapfill函数从较小的时间间隔(日)应该会延续到更长的时间间隔。

现在有一项关于改进 time_bucket 的调查:https://github.com/timescale/timescaledb/discussions/3258 - TmTron

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接