我似乎无法让groupArrayInsertAt聚合函数在Click house上工作.我通过零作为第二个参数,即位置,但它不断抛出错误.
我正在try 运行这个示例查询,它执行得很好,结果如下.
with dat_ as
(select 'K' as f, 'P' as g, 2 as h
union all
select 'K' as f, 'P' as g, 7 as h
union all
select 'K' as f, 'P' as g, 5 as h
union all
select 'G' as f, 'P' as g, 1 as h
union all
select 'G' as f, 'K' as g, 3 as h
union all
select 'G' as f, 'K' as g, 8 as h
union all
select 'P' as f, 'Y' as g, 2 as h
union all
select 'P' as f, 'Y' as g, 2 as h
union all
select 'P' as f, 'Y' as g, 2 as h
union all
select 'P' as f, 'J' as g, 5 as h)
,recs_ as (
select f, g, h, row_number() over(partition by f, g order by h desc) as rnk
from dat_
)
select f, g, groupArrayInsertAt(h, rnk) as arr_
from recs_
group by f, g
g f arr_
------------------
P G [0,1]
J P [0,5]
Y P [0,2,2,2]
K G [0,8,3]
P K [0,7,5,2]
然而,由于clickhouse数组的索引从零开始,因此我在结果中数组的开始处得到0,这不是我想要的输出.因此,我try 从下面的排名中扣除1,以防止生成的数组以零开始.
with dat_ as
(select 'K' as f, 'P' as g, 2 as h
union all
select 'K' as f, 'P' as g, 7 as h
union all
select 'K' as f, 'P' as g, 5 as h
union all
select 'G' as f, 'P' as g, 1 as h
union all
select 'G' as f, 'K' as g, 3 as h
union all
select 'G' as f, 'K' as g, 8 as h
union all
select 'P' as f, 'Y' as g, 2 as h
union all
select 'P' as f, 'Y' as g, 2 as h
union all
select 'P' as f, 'Y' as g, 2 as h
union all
select 'P' as f, 'J' as g, 5 as h)
,recs_ as (
select f, g, h, row_number() over(partition by f, g order by h desc) - 1 as rnk
from dat_
)
select f, g, groupArrayInsertAt(h, rnk) as arr_
from recs_
group by f, g
然而,当我运行此操作时,我会遇到以下错误.
SQL Error [43] [07000]: Code: 43. DB::Exception: Second argument of aggregate function groupArrayInsertAt must be unsigned integer. (ILLEGAL_TYPE_OF_ARGUMENT) (version 23.8.9.54 (official build))
我期望得到的输出如下.
g f arr_
------------------
P G [1]
J P [5]
Y P [2,2,2]
K G [8,3]
P K [7,5,2]
我已经判断了Clickhouse Docs并确认零是有效的UInt32,这是groupArrayInsertAt函数期望的参数类型.所以,我不明白为什么这不应该起作用.