简体   繁体   English

将 null 值联合到 Big Query 中的结构数组

[英]Union null values to a array of struct in Big Query

I have the following table in Big Query which has an array of struct type.我在 Big Query 中有下表,它有一个结构类型的数组。 I have to perform a union operation with a simple table and want to add null values in place of the nested columns.我必须对一个简单的表执行合并操作,并想添加 null 值来代替嵌套列。

Actual nested table example -实际嵌套表示例 - 在此处输入图像描述

Simple table (which needs to be union-ed)简单表(需要合并)

acc acc date日期 count数数
acc_6 acc_6 11/29/2022 11/29/2022 2 2个
acc_8 acc_8 11/30/2022 11/30/2022 3 3个

I tried the following query but it gives an error of incompatible types on the nested columns我尝试了以下查询,但它在嵌套列上给出了不兼容类型的错误

select * from actual_table 
union all
select acc, date, count,
array_agg(struct(cast(null as string) as device_id, cast(null as date) as to_date, cast(null as string) as from_date) as d
from simple_table

The resultant table should look like this -结果表应如下所示 - 在此处输入图像描述

Since d has a type of array of struct<string, string, string> , you need to write a null struct like below.由于d数组类型为 struct<string, string, string> ,因此您需要编写如下所示的null结构。

SELECT * FROM actual_table
 UNION ALL
SELECT *, [STRUCT(CAST(null AS STRING), CAST(null AS STRING), CAST(null AS STRING))] FROM simple_table;
  • [] is for array literal. []用于数组文字。 see Using array literals请参阅使用数组字面量
  • field names in null struct are optional cause they are already declared in actual_table before union all . null结构中的字段名称是可选的,因为它们已经在union all之前在actual_table中声明。
  • you can use STRING(null) instead of CAST(null AS STRING) which is a little bit concise.您可以使用STRING(null)而不是CAST(null AS STRING) ,这有点简洁。

Query results查询结果

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM