BigQuery - 相关子查询取消嵌套数组不起作用
Posted
技术标签:
【中文标题】BigQuery - 相关子查询取消嵌套数组不起作用【英文标题】:BigQuery - Correlated subquery unnesting array not working 【发布时间】:2020-05-14 11:28:14 【问题描述】:我正在尝试在 BigQuery 中加入数组元素,但收到以下错误消息:Correlated subqueries that reference other tables are not supported unless they can be de-correlated, such as by transforming them into an efficient JOIN.
假设我有两个映射表:
CREATE OR REPLACE TABLE `test.field_id_name` (
id STRING,
name STRING
) AS (
SELECT * FROM UNNEST(
[STRUCT("s1", "string1"),
STRUCT("s2", "string2"),
STRUCT("s3", "string3")]
)
)
CREATE OR REPLACE TABLE `test.field_values` (
id STRING,
name STRING
) AS (
SELECT * FROM UNNEST(
[STRUCT("v1", "val1"),
STRUCT("v2", "val2"),
STRUCT("v3", "val3")]
)
)
我有以下输入:
CREATE OR REPLACE TABLE `test.input` AS
SELECT [
STRUCT<id STRING, value ARRAY<STRING>>("s1", ["v1"]),
STRUCT("s2", ["v1"]),
STRUCT("s3", ["v1"])
] records
UNION ALL
SELECT [
STRUCT("s1", ["v1", "v2"]),
STRUCT("s2", ["v1", "v2"]),
STRUCT("s3", ["v1", "v2"])
]
UNION ALL
SELECT [
STRUCT("s1", ["v1", "v2", "v3"]),
STRUCT("s2", ["v1", "v2", "v3"]),
STRUCT("s3", ["v1", "v2", "v3"])
]
我正在尝试产生这个输出:
SELECT [
STRUCT<id_mapped STRING, value_mapped ARRAY<STRING>>("string1", ["val1"]),
STRUCT("string2", ["val1"]),
STRUCT("string3", ["val1"])
] records
UNION ALL
SELECT [
STRUCT("string1", ["val1", "val2"]),
STRUCT("string2", ["val1", "val2"]),
STRUCT("string3", ["val1", "val2"])
]
UNION ALL
SELECT [
STRUCT("string1", ["val1", "val2", "val3"]),
STRUCT("string2", ["val1", "val2", "val3"]),
STRUCT("string3", ["val1", "val2", "val3"])
]
但是,以下查询因相关子查询错误而失败。
SELECT
ARRAY(
SELECT
STRUCT(fin.name, ARRAY(SELECT fv.name FROM UNNEST(value) v JOIN test.field_values fv ON (v = fv.id)))
FROM UNNEST(records) r
JOIN test.field_id_name fin ON (fin.id = r.id)
)
FROM test.input
【问题讨论】:
请提供样本数据和预期结果。 嗨@MikhailBerlyant - 添加了具有所需转换的示例数据 对我来说看起来很乱输入 3) 你到目前为止的代码以及你有什么问题 感谢您的反馈。我已经修改了问题,希望更清楚! 快速浏览 - 现在看起来好多了。以后有时间会仔细看(除非有人已经回答了):o) 【参考方案1】:以下是 BigQuery 标准 SQL
#standardSQL
SELECT ARRAY_AGG(STRUCT(id AS id_mapped, val AS value_mapped)) AS records
FROM (
SELECT fin.name AS id, ARRAY_AGG(fv.name) AS val, FORMAT('%t', t) id1, FORMAT('%t', RECORD) id2
FROM `test.input` t,
UNNEST(records) record,
UNNEST(value) val
JOIN `test.field_id_name` fin ON record.id = fin.id
JOIN `test.field_values` fv ON val = fv.id
GROUP BY id, id1, id2
)
GROUP BY id1
如果应用于您问题的样本数据 - 返回您期望的准确输出
【讨论】:
不知道你是怎么想出这样的解决方案的。你是魔术师 请考虑对答案进行投票 - 这在 SO 上很重要! :o) 就像魔术师一样——当你在过去大约 40 多年的时间里日复一日地做这样的事情时——你不会做魔法——你只是机械地应用模式...... :o)以上是关于BigQuery - 相关子查询取消嵌套数组不起作用的主要内容,如果未能解决你的问题,请参考以下文章
BigQuery SQL:将视图 A 中的子查询作为嵌套表嵌入视图 B