问题描述
在一次 Afinity 网络研讨会中,他们给出了使用 argMin/argMax 聚合函数查找某个表中的第一个/最后一个值的示例。他们正在使用下表:
CREATE TABLE cpu_last_point_idle_agg (
created_date AggregateFunction(argMax,Date,DateTime),...
)
Engine = AggregatingMergeTree
然后他们创建物化视图:
CREATE MATERIALIZED VIEW cpu_last_point_idle_mw
TO cpu_last_point_idle_agg
AS SELECT
argMaxState(created_date,created_at) AS created_date,...
最后是视图:
CREATE VIEW cpu_last_point_idle AS
SELECT
argMaxMerge(created_date) AS created_date,...
我的桌子:
CREATE TABLE candles.ESM20_mthly_data (
ts DateTime Codec(Delta,LZ4),open AggregateFunction(argMin,DateTime,Float64),...
)
Engine = AggregatingMergeTree
PARTITION BY toYYYYMM(ts)
ORDER BY ts
PRIMARY KEY(ts);
我的物化视图:
CREATE MATERIALIZED VIEW candles.ESM20_mthly_mw
TO candles.ESM20_mthly_data
AS SELECT
ts,argMinState(ts,src.price) AS open,...
FROM source_table as src
GROUP BY toStartOfInterval(src.ts,INTERVAL 1 month) as ts;
我的观点:
CREATE VIEW candles.ESM20_mthly
AS SELECT
ts,argMinMerge(ts) as open,...
FROM candles.ESM20_mthly_mw
GROUP BY ts;
Code: 43. DB::Exception: Received from localhost:9000. DB::Exception: Illegal type DateTime('UTC') of argument for aggregate function with Merge suffix must be AggregateFunction(...).
我尝试使用 Date 和 DateTime,结果相同。如果我翻转 arg 和 value,它会起作用,但当然不会给我我想要的东西。这些聚合函数不再支持日期吗?我如何使它工作?
我正在使用 Connected to ClickHouse server version 20.12.3 revision 54442.
解决方法
首先 argMin(a,b) -- 当 b 是 min 时取 a。
--AggregateFunction(argMin,DateTime,Float64),++AggregateFunction(argMin,Float64,DateTime),--argMinState(ts,src.price) AS open,++argMinState(src.price,ts) AS open,
第二个问题是
--argMinMerge(ts) as open,++argMinMerge(open) as final_open,