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:基于另一个数组的数组总和的主要内容,如果未能解决你的问题,请参考以下文章