BigQuery:将重复的记录转换为列

时间:2019-09-25 10:49:15

标签: firebase google-bigquery nosql

我的团队决定使用重复记录架构将Firebase事件存储在BigQuery中:

BigQuery table schema

该表将如下所示:

event_name      | event_params.key | event_params.value.string_value
--------------------------------------------------------------------
element_clicked |       element_id |                       button-1
                |           result |                             ok
--------------------------------------------------------------------
element_clicked |       element_id |                       image-23
--------------------------------------------------------------------
screen_openend  |        screen_id |                  launch_screen

有没有一种方法可以让我将重复的记录转换为列,因此新表如下所示:

event_name      | element_id | result |     screen_id
-----------------------------------------------------
element_clicked |  button-1  |     ok |          NULL 
-----------------------------------------------------
screen_opened   |       NULL |   NULL | launch_screen 
-----------------------------------------------------
element_clicked |  image-34  |   NULL |          NULL

或者:还有其他简单的方法可以让我查询例如“具有名称element_clicked和element_id = button-1的事件或具有名称screen_opened和screen_id = launch_screen的事件”?

我开始尝试使用UNNEST函数,但是当event_params列表的行与行之间不同时,我认为它不会正确运行:

SELECT 
   event_name,
   ELEMENT_ID.value.string_value AS element_id,
   RESULT.value.string_value AS result,
   SCREEN_ID.value.string_value AS screen_id
FROM my_table, UNNEST(event_params) AS ELEMENT_ID, UNNEST(event_params) AS RESULT, UNNEST(event_params) AS SCREEN_ID
WHEN ELEMENT_ID.key = 'element_id' AND RESULT.key = 'result' AND SCREEN_ID.key = 'screen_id'

0 个答案:

没有答案