如何在 Timescale 中为现有 table 创建物化视图?

How to create a materialized view for existing table in Timescale?

当我尝试通过查询为现有 table 创建实体化视图时:

CREATE MATERIALIZED VIEW current_data_hourly
WITH (timescaledb.continuous) AS
SELECT id,
        time_bucket(INTERVAL  '1 hour', creation_time) AS creation_time,
       AVG(current_abs_1_avg),
       MAX(current_abs_1_max),
       MIN(current_abs_1_min)
FROM time_series.current_data
GROUP BY id, creation_time;

我得到:

ERROR:  continuous aggregate view must include a valid time bucket function
SQL state: XX000

有什么错误的建议吗?

在上面的视图查询中,输入列名称(在超表 time_series.current_data 中为 creation_time)和输出列名称(定义为 [=13=)之间的分组不明确].根据GROUP BYSELECT documentation中的描述:

In case of ambiguity, a GROUP BY name will be interpreted as an input-column name rather than an output column name.

creation_time 不是 time_bucker 表达式的别名。因此错误。

一种修复方法是使用 SELECT 子句中列的位置:

CREATE MATERIALIZED VIEW current_data_hourly
WITH (timescaledb.continuous) AS
SELECT id,
        time_bucket(INTERVAL  '1 hour', creation_time) AS creation_time,
       AVG(current_abs_1_avg),
       MAX(current_abs_1_max),
       MIN(current_abs_1_min)
FROM time_series.current_data
GROUP BY 1, 2;