【发布时间】:2019-05-20 15:02:54
【问题描述】:
假设我在 BigQuery 中有一些数据,其中包含一个嵌套的对象数组,如下所示:
{
"name" : "Bob",
"age": "24",
"customFields": [
{
"index": "1",
"value": "1.98"
},
{
"index": "2",
"value": "Nintendo"
},
{
"index": "3",
"value": "Yellow"
}
]
}
我只能取消嵌套这些数据,以便“索引”和“值”字段成为列:
+------+-----+-------+----------+
| name | age | index | value |
+------+-----+-------+----------+
| Bob | 24 | 1 | 1.98 |
| Bob | 24 | 2 | Nintendo |
| Bob | 24 | 3 | Yellow |
+------+-----+-------+----------+
在大多数情况下,这将是所需的输出,但由于我使用的数据是指 Google Analytics(分析)自定义维度,因此我需要一些更复杂的东西。我正在尝试获取要在数据出现的列的名称中使用的索引值,如下所示:
+------+-----+---------+----------+---------+
| name | age | index_1 | index_2 | index_3 |
+------+-----+---------+----------+---------+
| Bob | 24 | 1.98 | Nintendo | Yellow |
+------+-----+---------+----------+---------+
这可能吗?生成此输出所需的 SQL 查询是什么?它应该在列名中使用“索引”值,因为输出不会一直处于有序的“1,2,3,...”中。
【问题讨论】:
标签: google-bigquery