在使用SELECT AS STRUCT时,确保正确地命名所有所选字段,尤其是子查询中使用的字段。例如,如果您想要为结构中的每个字段指定名称,则可以按以下方式编写查询:
SELECT
STRUCT(
customer_id as id,
(
SELECT
ARRAY_AGG(
STRUCT(
order_id as id,
product_name as name,
quantity as qty
)
)
FROM
orders
WHERE
orders.customer_id = customers.customer_id
) as orders
) as customer
FROM
customers
此查询中,使用“as”对每个字段进行命名,并创建了一个名为“orders”的子查询。在子查询中,同样使用了“as”对结构中的每个字段进行了命名。
通过在SELECT AS STRUCT中正确地命名所有字段,可以确保在使用结果时能够轻松访问和处理结构化数据。