[英]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 ![]() |
date![]() |
count![]() |
---|---|---|
acc_6 ![]() |
11/29/2022 ![]() |
2 ![]() |
acc_8 ![]() |
11/30/2022 ![]() |
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
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 literalsnull
struct are optional cause they are already declared in actual_table
before union all . null
结构中的字段名称是可选的,因为它们已经在union all之前在actual_table
中声明。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.