Clickhouse:基于另一个数组的数组总和

Posted

技术标签:

【中文标题】Clickhouse:基于另一个数组的数组总和【英文标题】:Clickhouse: sum of arrays based on another array 【发布时间】:2020-06-02 12:48:38 【问题描述】:

假设你有下表:

ID  COUNTRIES                   UNITS
1   ['UK', 'Italy', 'France']   [2000, 1000, 1000]
1   ['France', 'Italy', 'UK']   [3000, 4000, 2000]
1   ['Italy']                   [2000]

ClickHouse 中有没有一种方法可以将COUNTRIES 列中每个国家/地区的RATios 列相加?类似sum(UNITS) group by COUNTRIES

预期结果:

ID  COUNTRIES                   TOTAL_UNITS
1   ['UK', 'Italy', 'France']   [4000, 4000, 7000]

代表代码:

SELECT data.1 ID, data.2 COUNTRIES, data.3 UNITS
FROM (
    SELECT arrayJoin([(1, ['UK', 'Italy', 'France'], [2000, 1000, 1000]), 
                    (1, ['France', 'Italy', 'UK'], [3000, 4000, 2000]), 
                    (1, ['Italy'], [2000])]) data);

【问题讨论】:

【参考方案1】:
select sumMap(COUNTRIES, UNITS) from (

SELECT data.1 ID, data.2 COUNTRIES, data.3 UNITS
FROM (
    SELECT arrayJoin([(1, ['UK', 'Italy', 'France'], [2000, 1000, 1000]), 
                    (1, ['France', 'Italy', 'UK'], [3000, 4000, 2000]), 
                    (1, ['Italy'], [2000])]) data)

)

┌─sumMap(COUNTRIES, UNITS)───────────────────┐
│ (['France','Italy','UK'],[4000,7000,4000]) │
└────────────────────────────────────────────┘




select (sumMap(COUNTRIES, UNITS) as x).1 a , x.2  b from (

SELECT data.1 ID, data.2 COUNTRIES, data.3 UNITS
FROM (
    SELECT arrayJoin([(1, ['UK', 'Italy', 'France'], [2000, 1000, 1000]), 
                    (1, ['France', 'Italy', 'UK'], [3000, 4000, 2000]), 
                    (1, ['Italy'], [2000])]) data)

)
┌─a───────────────────────┬─b────────────────┐
│ ['France','Italy','UK'] │ [4000,7000,4000] │
└─────────────────────────┴──────────────────┘




select groupArray(COUNTRY) COUNTRIES, groupArray(s) UNITS from (
select sum(UNIT) s, COUNTRY from (

SELECT data.1 ID, data.2 COUNTRIES, data.3 UNITS
FROM (
    SELECT arrayJoin([(1, ['UK', 'Italy', 'France'], [2000, 1000, 1000]), 
                    (1, ['France', 'Italy', 'UK'], [3000, 4000, 2000]), 
                    (1, ['Italy'], [2000])]) data)
) array join UNITS as UNIT, COUNTRIES as COUNTRY
group by COUNTRY)

┌─COUNTRIES───────────────┬─UNITS────────────┐
│ ['France','Italy','UK'] │ [4000,7000,4000] │
└─────────────────────────┴──────────────────┘

【讨论】:

以上是关于Clickhouse:基于另一个数组的数组总和的主要内容,如果未能解决你的问题,请参考以下文章

Clickhouse - 矩阵逐项加法:如何对二维数组求和?

将一个数组的唯一值映射到另一个数组的对应总和

聚合查询中由另一个字段分组的两个 int 数组的总和

从数组 Laravel 中获取元素总和

键/值数组的双调排序

比较 ClickHouse 行中的两个数组