【问题标题】:Using wildcard in bigquery project name在 bigquery 项目名称中使用通配符
【发布时间】:2022-01-23 02:06:23
【问题描述】:

我正在尝试从 SALES 数据集中的所有表中提取一些信息。我想为多个客户运行它。每个客户都有一个大的查询项目。波纹管查询返回我想要的伦敦客户。如何使用通配符对其他 10 个客户端执行相同的查询并将所有内容合并到一个表中?

SELECT *
FROM   london_prod_uk_eu.sales.__tables__ 

基本上,我想简化以下查询:

SELECT *
FROM   london_prod_uk_eu.sales.__tables__
UNION ALL
SELECT *
FROM   toronto_prod_can_us.sales.__tables__

SELECT *
FROM   rome_prod_it_eu.sales.__tables__
UNION ALL
SELECT *
FROM   madrid_prod_sp_eu.sales.__tables__ 

【问题讨论】:

    标签: sql google-bigquery


    【解决方案1】:

    考虑以下方法

    declare query array<string> default [];
    declare projects array<string>;
    set projects = [
      'london-prod-uk-eu',
      'toronto-prod-ca-us',
      'rome-prod-it-eu',
      'madrid-prod-sp-eu'
    ];
    
    for record IN (select project from unnest(projects) project) do
       set query =  query || [format('select * from `%s.SALES.__TABLES__`', record.project)];
    end for;
    
    execute immediate (select string_agg(line, ' union all ') from unnest(query) line); 
    

    【讨论】:

      猜你喜欢
      • 2018-02-24
      • 2019-01-27
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多