You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BigQuery中如何展开多个嵌套值并提取指定字段

提取嵌套数组中的指定字段

schema

我想要提取图片中高亮的两个字段:attributes.price.list.item.net 以及 attributes.price.list.item.listPrice.gross。

我尝试了以下代码,但它会展开整个list数组并返回所有列,其他展开方式还会报错,请问怎么正确展开这类多层嵌套数组?

SELECT attributes.price.list
FROM my_table LEFT JOIN UNNEST(attributes.price.list)

解决方案

根据你的schema结构,需要逐层处理嵌套的数组/结构体,以下是针对BigQuery的正确写法:

情况1:item是数组类型

如果list.item是嵌套数组,需要二次展开:

SELECT
  item.net,
  item.listPrice.gross
FROM my_table
LEFT JOIN UNNEST(attributes.price.list) AS list_item
LEFT JOIN UNNEST(list_item.item) AS item

情况2:item是结构体类型

如果list.item是单个结构体(非数组),直接访问字段即可:

SELECT
  list_item.item.net,
  list_item.item.listPrice.gross
FROM my_table
LEFT JOIN UNNEST(attributes.price.list) AS list_item

核心逻辑是先展开外层的attributes.price.list数组,给展开后的元素命名(比如list_item),再根据内层item的类型,选择直接访问字段或者再次展开数组,最终提取你需要的两个目标字段。

内容的提问来源于stack exchange,提问作者Aurangzeb Rathore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 10:54:36