BigQuery支持使用UNNEST函数解除多维数组的嵌套关系。以下是一个示例:
假设我们有一张名为test_table
的表,其中包含一个名为nested_array
的嵌套数组。每个元素都是另一个包含value1
和value2
的结构。
要解除嵌套数组,我们可以使用以下查询:
SELECT nested_array.value1, nested_array.value2
FROM `project.dataset.test_table` t, UNNEST(t.nested_array) AS nested_array
这将展开nested_array
中的所有元素,并将value1
和value2
作为新列返回。
如果我们想按value1
对结果进行分组,则可以修改查询以添加GROUP BY
:
SELECT nested_array.value1, AVG(nested_array.value2) AS avg_value2
FROM `project.dataset.test_table` t, UNNEST(t.nested_array) AS nested_array
GROUP BY nested_array.value1