BigQuery:将数组中的键/值对转换为列

时间:2020-11-02 03:31:43

标签: sql google-bigquery

我引用了此post,并希望将event_params字段中的选定键值对转换为列。我的桌子是这样的:

| user_id | event_params.key | event_params.value.string_value |
+---------+------------------+---------------------------------+
|  1      |               k1 |                             v11 |
|         +------------------+---------------------------------+
|         |               k2 |                             v12 |
|         +------------------+---------------------------------+
|         |               k3 |                             v13 |
+---------+------------------+---------------------------------+
|  1      |               k1 |                             v21 |
|         +------------------+---------------------------------+
|         |               k2 |                             v22 |
|         +------------------+---------------------------------+
|         |               k3 |                             v23 |
+---------+------------------+---------------------------------+
|  2      |               k1 |                             v31 |
|         +------------------+---------------------------------+
|         |               k2 |                             v32 |
|         +------------------+---------------------------------+
|         |               k3 |                             v33 |

每个大行在event_params字段内具有任意N个行,并且每个大行都可以重复,因为它可以由同一用户生成。我想保留此重复。

我想要的最终结果:

| user_id |  k1 |  k3 |
+---------+-----+-----+
|       1 | v11 | v13 |
+---------+-----+-----+
|       1 | v21 | v23 |
+---------+-----+-----+
|       2 | v31 | v33 |

到目前为止,我的查询是

SELECT 
  user_id, 
  IF(event_params.key = 'k1', event_params.value.string_value, NULL) AS k1,
  IF(event_params.key = 'k3', event_params.value.string_value, NULL) AS k3,
FROM `my-proj-id.analytics_xxxxx.events_20201030`
  , UNNEST(event_params) AS event_params
WHERE event_name='my-event-name'
为简便起见,上表中省略了

event_name

我当前的实现在整个列NULL中产生了k3,我认为这可能是由于UNNEST造成的,没有一个未嵌套的行将同时包含k1和k3在一起。如何转换为上面想要的最终结果?

注意:我想用标准SQL编写

2 个答案:

答案 0 :(得分:1)

以下是用于BigQuery标准SQL

#standardSQL
select user_id,
  (select value.string_value from t.event_params where key = 'k1') as k1,
  (select value.string_value from t.event_params where key = 'k3') as k3  
from `my-proj-id.analytics_xxxxx.events_20201030` t

如果要应用于您的问题的样本数据-输出为

enter image description here

答案 1 :(得分:0)

您可以在FROM子句中使用LEFT JOIN来执行此操作:

SELECT e.user_id, 
       event_k1.value.string_value AS k1,
       event_k3.value.string_value AS k3
FROM `my-proj-id.analytics_xxxxx.events_20201030` e LEFT JOIN
     UNNEST(e.event_params) event_k1
     ON event_k1.key = 'k1' LEFT JOIN
     UNNEST(e.event_params) event_k3
     ON event_k3.key = 'k3'
WHERE e.event_name = 'my-event-name';