我有旧桌子:
CREATE TABLE old_stats
(
id String,
results_count UInt64
)
ENGINE = AggregatingMergeTree
PRIMARY KEY (id);
新聚合表:
CREATE TABLE new_stats
(
id String,
results_count AggregateFunction(count)
)
ENGINE = AggregatingMergeTree
PRIMARY KEY (id);
如何从
new_stats
直接插入
old_stats
INSERT INTO new_stats
SELECT
id,
result_count
FROM old_stats;
不工作的解决方案:
countState(result_count) as result_count
破例Conversion from AggregateFunction(count, UInt8) to AggregateFunction(count) is not supported
arrayReduce('countState', range(1, results_count))
破例Conversion from AggregateFunction(count, UInt8) to AggregateFunction(count) is not supported
如果计数只是整数的总和,请使用
SimpleAggregateFunction
:
CREATE TABLE new_stats
(
`id` String,
`results_count` SimpleAggregateFunction(sum, UInt64)
)
ENGINE = AggregatingMergeTree
PRIMARY KEY id
注意以下插入的结果:
insert into old_stats values (1, 200);
insert into new_stats values (1,10);
SELECT * FROM new_stats FINAL
Query id: 718c2240-97ca-431c-b8cf-9690961100ad
┌─id─┬─results_count─┐
│ 1 │ 210 │
└────┴───────────────┘
并且...您上面的查询将正常工作(您将旧表插入新表的位置)
我在做另一个项目时才意识到答案。你在MV中的列是
AggregateFunction(count)
类型,所以你需要使用countState
函数向其中插入值。
尝试以下查询(它在我刚刚运行的简单测试中有效):
INSERT INTO new_stats SELECT
id,
countState(results_count) AS results_count
FROM old_stats
GROUP BY id