对数组进行分组和计数
Posted
技术标签:
【中文标题】对数组进行分组和计数【英文标题】:Group and count an Array 【发布时间】:2019-03-27 16:36:20 【问题描述】:从relaxo.tracks中选择arrayReduce('groupUniqArray', groupArray(browser));
arrayReduce
不适用于任意 lambda。有没有办法计算数组中出现元素的计数?喜欢
select groupArray(age) from customers;
:) [21, 40, 20, 20, 20, 30]
select arrayReduce('groupUniqArray', groupArray(age)) from customers;
:) [21, 40, 20, 30]
select arrayReduce('???', groupArray(age)) from customers;
:) [(21, 1), (40, 1), (20, 3), (30, 1)]
输出格式不是那么重要。我不想在这里使用 group-by/count,因为我想通过一个查询来聚合多个字段。
select
arrayReduce('???', groupArray(age)),
arrayReduce('???', groupArray(job)),
arrayReduce('???', groupArray(country))
from customers;
喜欢这个
【问题讨论】:
我目前的做法是使用 sumMap 和散列字符串 (murmurHash2_32) 【参考方案1】:只需对几个数组进行操作:
SELECT
groupArray(age) AS ages,
arrayReduce('groupUniqArray', ages) AS uniqAges,
arraySort(x -> x.1, arrayMap(x -> (x, countEqual(ages, x)), uniqAges)) AS resultAges,
groupArray(job) AS jobs,
arrayReduce('groupUniqArray', jobs) AS uniqJobs,
arraySort(x -> x.1, arrayMap(x -> (x, countEqual(jobs, x)), uniqJobs)) AS resultJobs,
groupArray(country) AS countries,
arrayReduce('groupUniqArray', countries) AS uniqCountries,
arraySort(x -> x.1, arrayMap(x -> (x, countEqual(countries, x)), uniqCountries)) AS resultCountries
FROM test.test4
FORMAT Vertical
【讨论】:
以上是关于对数组进行分组和计数的主要内容,如果未能解决你的问题,请参考以下文章
在 jQuery 中按数据属性对 HTML 元素进行分组和计数