Pilih arrayReduce('groupUniqArray', groupArray(browser)) dari relaxo.tracks;

arrayReduce tidak berfungsi dengan lambda arbitrer. Apakah ada cara untuk menghitung jumlah elemen kemunculan dalam array? Suka

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)]

Format output tidak begitu penting. Saya tidak ingin menggunakan group-by/count di sini karena saya ingin memiliki agregat untuk beberapa bidang dengan satu kueri.

select 
  arrayReduce('???', groupArray(age)),
  arrayReduce('???', groupArray(job)),
  arrayReduce('???', groupArray(country))
from customers;

Seperti ini

0
fl00r 27 Maret 2019, 19:36

1 menjawab

Jawaban Terbaik

Ambil saja beberapa manipulasi array:

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
1
vladimir 28 Maret 2019, 23:01