BigQuery SQL - a way to pass values from more than one row and more than one column to User Defined Function -
i want create user defined function, (create temporary function
) in bigquery standard sql accept values aggregated bunch of rows.
my schema , table similar this:
| c1 | c2 | c3 | c4 | |=======|=======|=======|=======| | 1 | 1-1 | 3a | 4a | | 1 | 1-1 | 3b | 4b | | 1 | 1-1 | 3c | 4c | | 1 | 1-2 | 3d | 4d | | 2 | 2-1 | 3e | 4e | | 2 | 2-1 | 3f | 4f | | 2 | 2-2 | 3g | 4g | | 2 | 2-2 | 3h | 4h |
i can't change original schema made of nested or array fields.
i want group c1
, c2
, pass values of c3
, c4
function, while being able match between values c3
, c4
each row. 1 way of doing using array_agg
, pass values array
, array_agg
non-deterministic values c3
, c4
might come different orders source table. example
:
create temporary function tempconcatstrfunction(c3 array<string>, c4 array<string>) returns string language js """ return c3 .map((item, index) => [ item, c4[index] ].join(',')) .join(','); """; t ( select c1, c2, array_agg(c3) c3, array_agg(c4) c4 group c1, c2 ) select c1, c2, tempconcatstrfunction(c3, c4) str t
the result should be:
| c1 | c2 | str | |=======|=======|======================| | 1 | 1-1 | 3a,4a,3b,4b,3c,4c | | 1 | 1-2 | 3d,4d | | 2 | 2-1 | 3e,4e,3f,4f | | 2 | 2-2 | 3g,4g,3h,4h |
any ideas how achieve such results?
any ideas how achieve such results?
i understand question how keep c3 , c4 match each other in final string. how keep super simple below
select c1, c2, string_agg(concat(c3, ',', c4)) str yourtable group c1, c2
Comments
Post a Comment