bigquery嵌套&重复的字段查询

时间:2016-11-04 07:27:00

标签: google-bigquery

我正在使用bigquery为Firebase分析查询做一些示例。 我有这样的记录。

event_dim:[

  “name”:”MOVE”,

  “params”:[

     {

         “key”:”npc_id”,

         “value”:{

              "string_value": “11”,

              "int_value": null,

              "float_value": null,

              "double_value": null

            }

     },

     {

         “key”:”posX”,

         “value”:{

              "string_value": null,

              "int_value": null,

              "float_value": null,

              "double_value": “10.0”

            }

     },

     {

         “key”:”posY”,

         “value”:{

              "string_value": null,

              "int_value": null,

              "float_value": null,

              "double_value": “12.0”

            }

     },

]

如何查询此数据,结果如下?

名称| npc_id | posX | POSY

MOVE | 11 | 10.0 | 12.0

1 个答案:

答案 0 :(得分:3)

工作查询:

#StandardSQL
SELECT * FROM (
  SELECT 
    e.name, 
    (SELECT value.string_value FROM UNNEST(e.params) WHERE key = "item_name") AS item_name,
    (SELECT value.int_value FROM UNNEST(e.params) WHERE key = "value") AS value
  FROM 
    `firebase-analytics-sample-data.ios_dataset.app_events_20160601`,
    UNNEST(event_dim) as e
)
WHERE item_name IS NOT null

秘密是UNNEST嵌套事件记录,然后是UNNEST嵌套参数,寻找具有正确密钥的记录。