Clickhouse - 矩阵入口加法:如何对二维数组求和? [英] Clickhouse - Matrix entrywise addition: how to sum 2 dimensional arrays?

查看:85
本文介绍了Clickhouse - 矩阵入口加法:如何对二维数组求和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Clickhouse 数据库计算表中矩阵的入口总和的正确和最快的方法是什么?

What is the proper and fastest way to calculate entrywise sum of matrices in table using Clickhouse database?

我找到了这样的解决方案.但在我看来这还不够理想.还有一点,我需要为一维的每个元素指定 sumForEach().

I have found such solution. But it seems to me it's not optimal enough. And one more, I need to specify sumForEach() for each element of the one dimension.

select array(sumForEach(matrix[1]), sumForEach(matrix[2])) from (
 select 1 as id, [[1,3], [2,4]] as matrix
 union all
 select 2 as id, [[2,4], [3,5]] as matrix
 union all
 select 3 as id, [[1,2], [1,0]] as matrix
)

因此,我想看到一行带有结果矩阵

as a result, I want to see one row with the resulting matrix

┌─m─────────────┐
│[[4,9],[6,9]]  │
└───────────────┘

推荐答案

聚合函数组合器 可以多次应用,有助于简化查询:

Aggregate function combinators can be applied many times, that help to simplify query:

SELECT sumForEachForEach(matrix)
FROM 
(
    SELECT 
        1 AS id, 
        [[1, 3], [2, 4]] AS matrix
    UNION ALL
    SELECT 
        2 AS id, 
        [[2, 4], [3, 5]] AS matrix
    UNION ALL
    SELECT 
        3 AS id, 
        [[1, 2], [1, 0]] AS matrix
)
/*
┌─sumForEachForEach(matrix)─┐
│ [[4,9],[6,9]]             │
└───────────────────────────┘
*/

这篇关于Clickhouse - 矩阵入口加法:如何对二维数组求和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆