在 BigQuery 上连接嵌套字段的值
Concatenate the values of a nested field on BigQuery
假设我有以下架构:
[
{
'name': 'id',
'type': 'INTEGER'
}
{
'name': 'record',
'type': 'RECORD',
'fields': [
{
'name': 'repeated',
'type': 'STRING',
'mode': 'REPEATED'
}
]
}
]
以及以下数据:
+--------------------+
|id |record.repeated|
+--------------------+
|1 |'a' |
| |'b' |
| |'c' |
+--------------------+
|2 |'a' |
| |'c' |
+--------------------+
|3 |'d' |
+--------------------+
我需要的是创建一个查询 returns 这个:
+--------------------+
|id |record.repeated|
+--------------------+
|1 |'a,b,c' |
+--------------------+
|2 |'a,c' |
+--------------------+
|3 |'d' |
+--------------------+
换句话说,我需要查询允许我使用分隔符(在本例中为逗号)连接嵌套字段的值。类似于 MySQL 的 GROUP_CONCAT 函数,但在 BigQuery 上。
相关观点:Concat all column values in sql
这可能吗?
谢谢。
很简单
select group_concat(record.repeated) from table
publicdata 的一个例子是
SELECT group_concat(payload.shas.encoded)
FROM [publicdata:samples.github_nested]
WHERE repository.url='https://github.com/dreamerslab/workspace'
对于标准 sql:
select id, string_agg(record.field)
from your_table, unnest(record)
或
select id, string_agg(record.field)
from your_table left join unnest(record)
假设我有以下架构:
[
{
'name': 'id',
'type': 'INTEGER'
}
{
'name': 'record',
'type': 'RECORD',
'fields': [
{
'name': 'repeated',
'type': 'STRING',
'mode': 'REPEATED'
}
]
}
]
以及以下数据:
+--------------------+
|id |record.repeated|
+--------------------+
|1 |'a' |
| |'b' |
| |'c' |
+--------------------+
|2 |'a' |
| |'c' |
+--------------------+
|3 |'d' |
+--------------------+
我需要的是创建一个查询 returns 这个:
+--------------------+
|id |record.repeated|
+--------------------+
|1 |'a,b,c' |
+--------------------+
|2 |'a,c' |
+--------------------+
|3 |'d' |
+--------------------+
换句话说,我需要查询允许我使用分隔符(在本例中为逗号)连接嵌套字段的值。类似于 MySQL 的 GROUP_CONCAT 函数,但在 BigQuery 上。
相关观点:Concat all column values in sql
这可能吗?
谢谢。
很简单
select group_concat(record.repeated) from table
publicdata 的一个例子是
SELECT group_concat(payload.shas.encoded)
FROM [publicdata:samples.github_nested]
WHERE repository.url='https://github.com/dreamerslab/workspace'
对于标准 sql:
select id, string_agg(record.field)
from your_table, unnest(record)
或
select id, string_agg(record.field)
from your_table left join unnest(record)