Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It is possible to create flat array from query results in clickhouse?

Tags:

clickhouse

Query

select *
from (
    select [1] a
    union all
    select [1,2] a
    union all
    select [2] a
) q;

Result (3 rows)

[1]
[1,2]
[2]

Expected result (1 row)

[1,1,2,2]

It is possible ?

like image 393
cetver Avatar asked Sep 04 '25 16:09

cetver


2 Answers

Alternate way using Array-combinator:

SELECT groupArrayArray(*)
FROM 
(
    SELECT [1] AS a
    UNION ALL
    SELECT [1, 2] AS a
    UNION ALL
    SELECT [2] AS a
) AS q
/* result
┌─groupArrayArray(a)─┐
│ [1,1,2,2]          │
└────────────────────┘
*/
like image 76
vladimir Avatar answered Sep 07 '25 16:09

vladimir


Yes, sure. You just need a couple of array functions.

select arrayFlatten(groupArray(*))
from (
    select [1] a
    union all
    select [1,2] a
    union all
    select [2] a
) q;

groupyArray gives you [[1],[1,2],[2]], essentially grouping all the results in one array. arrayFlatten flattens the above array, resulting in [1,1,2,2].

like image 45
Matthew Formosa Avatar answered Sep 07 '25 16:09

Matthew Formosa