如何在BigQuery SQL中展平嵌套数组结构体?报错求助
问题解决指南
问题情况
- 表结构:
event是数组类型,每个数组元素为结构体,包含messageid(字符串)和reason(数组,元素为带reasonCode、reasonDesc的结构体) - 当前SQL语句:
select id, event.messageid as test_message, event[safe_offset(1)] from dataset.table where event.reason.reasoncode='damaged1';
- 报错信息:
Cannot access field reasoncode on a value with type ARRAY<STRUCT<reasonCode STRING,reasonDesc STRING>> at[9.29]
- 需求:筛选出
reason数组中存在reasonCode='damaged1'的记录,输出id、对应messageid、event数组的第二个元素
报错原因
event.reason 是数组类型,不是单个结构体,直接用 .reasoncode 访问会报错——数组本身没有这个字段,只有数组内的元素才包含该字段。
两种解决办法(适用于BigQuery)
办法1:保留原数组结构,用EXISTS筛选
适合不需要展开原数组,仅需过滤符合条件记录的场景:
select id, event[safe_offset(0)].messageid as test_message, -- 取event数组第一个元素的messageid event[safe_offset(1)] as second_event_item from dataset.table where exists ( select 1 from unnest(event) as event_item cross join unnest(event_item.reason) as reason_item where reason_item.reasonCode = 'damaged1' );
办法2:先展开数组再筛选(适合需处理展开后数据的场景)
如果需要对展开后的元素做更多操作,可先拆分数组过滤,再去重避免重复记录:
select t.id, event_item.messageid as test_message, t.event[safe_offset(1)] as second_event_item from dataset.table t cross join unnest(t.event) as event_item cross join unnest(event_item.reason) as reason_item where reason_item.reasonCode = 'damaged1' group by t.id, t.event, event_item.messageid;
小提示
safe_offset(n)用于安全访问数组第n个元素(从0开始计数),即使数组长度不足也不会触发报错- 两种写法均可实现需求,可根据实际场景选择
内容的提问来源于stack exchange,提问作者Kay
相关产品推荐
相关产品推荐

