Reputation: 578
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?
Upvotes: 5
Views: 6796
Reputation: 77
Another solution to your problem
SELECT arrayDistinct(arrayFlatten(groupArray(array_field)))
FROM my_table
Upvotes: 3
Reputation: 1734
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'] │
└──────────────────────────────────┘
Upvotes: 8
Reputation: 578
Found a solutions that works for me:
SELECT DISTINCT arrayJoin(array_field)
FROM my_table
Upvotes: 2