【问题标题】:How to rearrange/reorder nested repeated column in Bigquery when UNION tablesUNION表时如何在Bigquery中重新排列/重新排序嵌套的重复列
【发布时间】:2026-01-25 07:55:01
【问题描述】:

我有两个架构相似但顺序不同的 Bigquery 表

当使用下面的查询合并这两个表时,

select id, timestamp, products, count_total_visit, count_unique_session from table1
union all
select id, timestamp, products, count_total_visit, count_unique_session from table2

这是我得到的错误

UNION ALL 中的第 3 列具有不兼容的类型:ARRAY>, ARRAY> at [...]

如何重新排列嵌套重复字段的架构顺序,以便通过保持架构格式与源表相同的方式合并2张表?

【问题讨论】:

    标签: google-bigquery


    【解决方案1】:

    下面会解决问题

    select id, timestamp, products, count_total_visit, count_unique_session from table1
    union all
    select * replace (array( 
        select as struct * replace (array( 
            select as struct paiment_method, count_total_trx, sum_total_gbv, sum_total_revenue
              from product.details
            ) as details
          )
        from t.products product
      ) as products)
    from table2 t
    

    【讨论】:

    • 酷。这很简单,因为以前我认为必须先 UNNEST 才能重新排列列,然后再次创建嵌套。感谢您的回答。