如何在 BigQuery 中获取多个 event_params 作为列
Posted
技术标签:
【中文标题】如何在 BigQuery 中获取多个 event_params 作为列【英文标题】:How to get multiple event_params as column in BigQuery 【发布时间】:2019-04-10 11:41:35 【问题描述】:我需要什么:事件的总发生次数以及与此事件相关的数据。
我尝试了什么:在设备中我向 Firebase Analytics 发送了一些事件。事件看起来像:
event_name = "business_global_view_profile"
带参数:
business_global_name :String
business_global_city:String
business_global_ID:String
所以在 BigQuery 中我得到了类似的东西(在更多不需要的值和事件之间):
我只需要获取我发送的参数,所以我需要丢弃所有的东西,比如ga_session_id
、firebase_event_origin
,等等......
所以首先我尝试从所有其他事件中“过滤”我需要的事件:
SELECT params.key AS ParamKey, params.value.string_value as Value
FROM `xxxxxx.xxxxx.xxxxx`,
UNNEST(event_params) as params
WHERE event_name = "business_global_view_profile"
之后,我得到一个类似的表格:
此时我实现了通过 ID 获取出现次数
SELECT Value AS BusinessID, COUNT(Value) Occurrences
FROM(SELECT params.key AS ParamKey, params.value.string_value as Value
FROM `xxxxxx.xxxxx.xxxxx`,
UNNEST(event_params) as params
WHERE event_name = "business_global_view_profile"
AND event_date = "20190409")
WHERE ParamKey = "business_global_ID"
Group by Value
但这就是我迷路的地方,因为我需要的是这样的:
|BusinessID | Occurrences | BusinessName | BusinessCity |
在哪里
BusinessName = business_global_name
BusinessCity = business_global_city
如何将所有这些信息汇总在一起?当然有更短的方法可以做到这一点,但我没有用 SQL 做太多工作
【问题讨论】:
【参考方案1】:我不太确定我是否正确地满足了您的要求,但这里有一个可能的解决方案。为简单起见,我使用了以下架构:
并提出以下查询:
SELECT occ_count.BusinessID, occ_count.Occurrences, flat_data.BusinessName, flat_data.BusinessCity
FROM
(
SELECT params.value AS BusinessID, count(params.value) AS Occurrences
FROM `XXXX.XXXX.XXXX`, UNNEST(event_params) as params
WHERE event_name = 'business_global_view_profile'
AND event_date = "20190409"
AND params.key = 'business_global_id'
GROUP BY params.value
) AS occ_count
JOIN
(
SELECT DISTINCT arr1.value AS BusinessID, arr2.value AS BusinessName, arr3.value as BusinessCity
FROM `XXXX.XXXX.XXXX`, UNNEST(event_params) as arr1, UNNEST(event_params) arr2, UNNEST(event_params) arr3
WHERE arr2.key = 'business_global_name'
AND arr3.key = 'business_global_city'
) AS flat_data
ON occ_count.BusinessID = flat_data.BusinessID
请注意,我假设对于任何business_global_id
,都有business_global_name
和business_global_city
的单一组合。为了简单起见,我还从event_params.value
而不是event_params.value.string_value
中获取值,如前所述。
【讨论】:
以上是关于如何在 BigQuery 中获取多个 event_params 作为列的主要内容,如果未能解决你的问题,请参考以下文章
如何简化和扩展 Firebase 数据库上的 BigQuery 子过滤 event_params
如何在 Google BigQuery 中使用 UNNEST 函数获取 COUNT?
如果标准 SQL 不提供 _PARTITIONTIME 字段,我如何在 BigQuery 中查询流缓冲区