Supose I have the following schema:
[
{
'name': 'id',
'type': 'INTEGER'
}
{
'name': 'record',
'type': 'RECORD',
'fields': [
{
'name': 'repeated',
'type': 'STRING',
'mode': 'REPEATED'
}
]
}
]
And the following data:
+--------------------+
|id |record.repeated|
+--------------------+
|1 |'a' |
| |'b' |
| |'c' |
+--------------------+
|2 |'a' |
| |'c' |
+--------------------+
|3 |'d' |
+--------------------+
What I need is to create a query that returns this:
+--------------------+
|id |record.repeated|
+--------------------+
|1 |'a,b,c' |
+--------------------+
|2 |'a,c' |
+--------------------+
|3 |'d' |
+--------------------+
In other words, I need to query that allows me to concatenate the values of a nested field using a separator (in this case, comma). Something like the GROUP_CONCAT function of MySQL, but on BigQuery.
Related idea: Concat all column values in sql
Is that possible?
Thanks.
Note: You can also use the || concatenation operator to concatenate values into a string.
BigQuery automatically flattens nested fields when querying. To query a column with nested data, each field must be identified in the context of the column that contains it. For example: customer.id refers to the id field in the customer column.
OFFSET means that the numbering starts at zero, ORDINAL means that the numbering starts at one. A given array can be interpreted as either 0-based or 1-based. When accessing an array element, you must preface the array position with OFFSET or ORDINAL , respectively; there is no default behavior.
It's very simple
select group_concat(record.repeated) from table
an example from publicdata is
SELECT group_concat(payload.shas.encoded)
FROM [publicdata:samples.github_nested]
WHERE repository.url='https://github.com/dreamerslab/workspace'
For standard sql:
select id, string_agg(record.field)
from your_table, unnest(record)
or
select id, string_agg(record.field)
from your_table left join unnest(record)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With