Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to join all arrays in clickhouse column and then filter for duplicates?

Tags:

clickhouse

I have a clickhouse table with one of it columns being Array(T). When I run SELECT array_field FROM my_table I get the following:

1 | {a, b, c}
--------------
2 | {a, b}
--------------
3 | {d, e}

I need to find a way to get a list of unique values in all of that lists, just like that:

{a, b, c, d, e}

How can I do that?

like image 985
Александр Свито Avatar asked Apr 03 '19 15:04

Александр Свито


3 Answers

To get the same in array in one row: use groupUniqArray with -Array combinator. Check docs

SELECT *
FROM my_table 

┌─array_field───┐
│ ['a','b','c'] │
│ ['a','b']     │
│ ['d','e']     │
└───────────────┘

3 rows in set. Elapsed: 0.001 sec. 

SELECT DISTINCT arrayJoin(array_field)
FROM my_table 

┌─arrayJoin(array_field)─┐
│ a                      │
│ b                      │
│ c                      │
│ d                      │
│ e                      │
└────────────────────────┘

SELECT groupUniqArrayArray(array_field)
FROM my_table 

┌─groupUniqArrayArray(array_field)─┐
│ ['c','e','d','a','b']            │
└──────────────────────────────────┘

like image 124
filimonov Avatar answered Sep 22 '22 14:09

filimonov


Found a solutions that works for me:

SELECT DISTINCT arrayJoin(array_field)
FROM my_table
like image 37
Александр Свито Avatar answered Sep 23 '22 14:09

Александр Свито


Another solution to your problem

SELECT arrayDistinct(arrayFlatten(groupArray(array_field)))
FROM my_table 
like image 43
kozulov Avatar answered Sep 23 '22 14:09

kozulov