我似乎无法让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函数期望的参数类型.所以,我不明白为什么这不应该起作用.

推荐答案

linkHouse将rnk的类型推断为Int64:

WITH
    dat_ AS
    (
        SELECT
            f,
            g,
            h
        FROM VALUES('f String, g String, h Int32', ('K', 'P', 2), ('K', 'P', 7), ('K', 'P', 5), ('G', 'P', 1), ('G', 'K', 3), ('G', 'K', 8), ('P', 'Y', 2), ('P', 'Y', 2), ('P', 'Y', 2), ('P', 'J', 5))
    ),
    recs_ AS
    (
        SELECT
            f,
            g,
            h,
            row_number() OVER (PARTITION BY f, g ORDER BY h DESC) - 1 AS rnk
        FROM dat_
    )
SELECT toTypeName(rnk)
FROM recs_
LIMIT 1

/*
┌─toTypeName(rnk)─┐
│ Int64           │
└─────────────────┘
*/

它需要显式转换为UInt32或其他无符号类型:

WITH
    dat_ AS
    (
        SELECT
            f,
            g,
            h
        FROM VALUES('f String, g String, h Int32', ('K', 'P', 2), ('K', 'P', 7), ('K', 'P', 5), ('G', 'P', 1), ('G', 'K', 3), ('G', 'K', 8), ('P', 'Y', 2), ('P', 'Y', 2), ('P', 'Y', 2), ('P', 'J', 5))
    ),
    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, toUInt32(rnk)) AS arr_
FROM recs_
GROUP BY
    f,
    g

/*
┌─f─┬─g─┬─arr_────┐
│ G │ P │ [1]     │
│ P │ J │ [5]     │
│ P │ Y │ [2,2,2] │
│ G │ K │ [8,3]   │
│ K │ P │ [7,5,2] │
└───┴───┴─────────┘
*/

Sql相关问答推荐

如何在VB.NET中使用MS SYS根据开始和结束期间日期进行查询

如何在SQL查询中只比较日期时间的年份和月份(而忽略日期比较)?

如何将资源密集型自连接转换为更快的查询?

当我们加入两个表时,我们可以省略GROUP BY中的列名吗?

一个SQL查询将在需要的地方多次返回同一成员

获得第三名或最老的记录

带上最后日期(结果)

在Postgres中合并相似的表

同一表的Oracle SQL更新(含YTD合计)

此过程如何在不引用传递的参数值的情况下执行工作?

Postgres jsonpath运算符的变量替换,如_regex?

提取连续时间戳范围的SQL

查找滑动窗口框架中的最大和最小列值

在 Postgres 中将结果按几十年划分

复制SQL Server临时表

为什么 get_json_object() 无法从存储在 Hive SQL 表中的 JSON 中提取值?

T-SQL 查询计算日期在其他列中定义的日期之间绑定的行数

在 Microsoft SQL Server 中,如何只为特定值保留不同的行?

SQL Server Where 条件

ACCESS SQL - 有没有办法使用通配符仅 Select 字段的特定部分?