google-bigquery - BigQuery 脚本: running a SQL query based on each row included in a table column

标签 google-bigquery bigquery-udf

我的源表包括url_idSQL [标准sql,字符串]

enter image description here

我想运行字符串列“SQL”中的每个 SQL 语句,并将结果与​​相应的 url_id 连接起来

预期结果

enter image description here

更新

为了使其易于重现,我从公共(public) BQ 导入了类似的架构:

bigquery-public-data.covid19_italy.data_by_province

enter image description here

如果我运行

select string_agg(
    format('select %i as url_id, (%s) as simpleSKU', url_id, sql), 
    ' union all '   )

FROM `xxx.staging.test_query

它返回 enter image description here

最佳答案

考虑下面

execute immediate (
  select 'select url_id, region_name from (' || string_agg(
    format('select %i as url_id, array(%s) as region_names', url_id, sql), 
    ' union all '
  ) || '), unnest(region_names) as region_name'
  from your_table
);   

如果应用于您问题中的示例数据 - 输出它

enter image description here

关于google-bigquery - BigQuery 脚本: running a SQL query based on each row included in a table column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65877988/

相关文章:

google-analytics - BigQuery 数据与 Google Analytics

python - BigQuery 到 Hadoop 集群 - 如何传输数据?

sql - BigQuery GroupBy 与 STRUCT

sql - 在 BigQuery 中查找重叠的时间段

google-bigquery - 用于删除字符串中的重音符号/变音符号的 BigQuery UDF

google-bigquery - BigQuery 使用动态键和值提取嵌套 JSON

google-cloud-platform - 使用 Deployment Manager 在 BigQuery 中创建 'external table' 时出现“403 Permission denied while getting Drive credentials”

google-bigquery - DBT 从不同的 Google 项目中选择 Big Query 表