Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate columns in alphabetic order in BigQuery

I have a table in BigQuery:

|c1|c2|c3|
----------
|a1|'b'|c1|
----------
|a1|'a'|c2|
----------

and I want to group by c1 and array_agg c2 and have a table like below:

 c1| c2
--------
 a1|'ab'

which concatenate the c2 column and save the alphabetic order. can someone help me with this?

like image 219
Maryam Abdoli Avatar asked Nov 16 '22 11:11

Maryam Abdoli


1 Answers

try the following. you can use order by inside string_agg. here is the documentation.

select
    c1,
    string_agg(c2 order by c2) as c2
from yourTable
group by
    c1
like image 176
zealous Avatar answered Dec 10 '22 11:12

zealous