无法按 BigQuery/Firebase 中的内容对购买者进行排序
Posted
技术标签:
【中文标题】无法按 BigQuery/Firebase 中的内容对购买者进行排序【英文标题】:Cannot sort purchasers by content in BigQuery/Firebase 【发布时间】:2017-06-30 12:29:42 【问题描述】:我正在使用 Firebase 分析移动应用。 Firebase 中的数据会导出到 BigQuery。
我正在尝试使用以下用标准 SQL 编写的查询来计算与特定内容相关的购买者:
SELECT COUNT(user_dim.app_info.app_instance_id) AS purchasers
,eventParams.value.string_value AS eventName
,event.DATE AS eventDate
FROM `id.TABLE.app_events_ * `
,UNNEST(event_dim) AS event
,UNNEST(event.params) AS eventParams
WHERE (
event.NAME = 'select_content'
AND eventParams.KEY = 'item_id'
)
AND user_dim.app_info.app_instance_id IN (
SELECT [user_dim.app_info.app_instance_id]
FROM `id.TABLE.app_events_ * `
,UNNEST(event_dim) AS event
,UNNEST(event.params) AS eventParams
WHERE event.NAME = 'in_app_purchase'
AND eventParams.KEY = 'quantity'
)
GROUP BY eventDate
,eventName
ORDER BY eventDate DESC
但我收到错误消息:'Cannot execute IN subquery with uncomparable types STRING and ARRAY'
我尝试在“IN”运算符之后使用“UNNEST”函数。 但后来我得到了错误:'标量子查询产生了多个元素'
请帮我找出我的错误在哪里。
【问题讨论】:
【参考方案1】:您只需要按顺序删除括号,查询即可工作:
AND user_dim.app_info.app_instance_id IN (
SELECT user_dim.app_info.app_instance_id
FROM `id.TABLE.app_events_ * `
,UNNEST(event_dim) AS event
,UNNEST(event.params) AS eventParams
WHERE event.NAME = 'in_app_purchase'
AND eventParams.KEY = 'quantity'
)
问题是您通过在列名周围放置方括号来创建数组。
【讨论】:
感谢您的回复!【参考方案2】:也许您可以在消除此错误的同时优化您的查询,如下所示:
WITH data AS(
SELECT STRUCT<user_id STRING, app_info STRUCT<app_instance_id STRING> > ('user_1', STRUCT('id_1' as app_instance_id)) user_dim, ARRAY<STRUCT<name STRING, date STRING, params ARRAY<STRUCT<key STRING, value STRUCT<string_value STRING> >> > > [STRUCT('select_content' as name, "20170610" as date, [STRUCT('item_id' as key, STRUCT('str_value1' as string_value) as value), STRUCT('item_id' as key, STRUCT('str_value2' as string_value) as value)] as params), STRUCT('in_app_purchase' as name, "20170610" as date, [STRUCT('quantity' as key, STRUCT('str_value1' as string_value) as value)] as params), STRUCT('select_content' as name, "20170611" as date, [STRUCT('item_id' as key, STRUCT('str_value1' as string_value) as value)] as params), STRUCT('select_content' as name, "20170611" as date, [STRUCT('item_id' as key, STRUCT('str_value2' as string_value) as value)] as params)] event_dim UNION ALL
SELECT STRUCT<user_id STRING, app_info STRUCT<app_instance_id STRING> > ('user_2', STRUCT('id_2' as app_instance_id)) user_dim, ARRAY<STRUCT<name STRING, date STRING, params ARRAY<STRUCT<key STRING, value STRUCT<string_value STRING> >> > > [STRUCT('select_content' as name, "20170610" as date, [STRUCT('item_id' as key, STRUCT('str_value1' as string_value) as value), STRUCT('item_id' as key, STRUCT('str_value2' as string_value) as value)] as params), STRUCT('in_app_purchase' as name, "20170610" as date, [STRUCT('quantity' as key, STRUCT('str_value2' as string_value) as value)] as params), STRUCT('select_content' as name, "20170612" as date, [STRUCT('item_id' as key, STRUCT('str_value2' as string_value) as value)] as params), STRUCT('select_content' as name, "20170610" as date, [STRUCT('item_id' as key, STRUCT('str_value2' as string_value) as value)] as params)] event_dim union all
SELECT STRUCT<user_id STRING, app_info STRUCT<app_instance_id STRING> > ('user_2', STRUCT('id_2' as app_instance_id)) user_dim, ARRAY<STRUCT<name STRING, date STRING, params ARRAY<STRUCT<key STRING, value STRUCT<string_value STRING> >> > > [STRUCT('select_content' as name, "20170610" as date, [STRUCT('item_id' as key, STRUCT('str_value1' as string_value) as value)] as params), STRUCT('select_content' as name, "20170612" as date, [STRUCT('item_id' as key, STRUCT('str_value2' as string_value) as value)] as params), STRUCT('select_content' as name, "20170610" as date, [STRUCT('item_id' as key, STRUCT('str_value2' as string_value) as value)] as params)] event_dim
)
SELECT
date,
eventName,
COUNT(1) purchases
FROM(
SELECT
ARRAY(SELECT AS STRUCT event.date date, params.value.string_value eventName FROM UNNEST(event_dim) event, UNNEST(event.params) params GROUP BY date, eventName) data
FROM data
WHERE EXISTS(SELECT 1 FROM UNNEST(event_dim) event WHERE event.name = 'in_app_purchase' AND (SELECT COUNT(1) FROM UNNEST(event.params) params WHERE params.key = 'quantity') > 0)
),
UNNEST(data)
GROUP BY
date, eventName
它不仅应该运行得更快,而且应该更便宜,因为它查询的数据列更少。
【讨论】:
【参考方案3】:我创建了查询,帮助我按内容对购买者进行排序。它在旧版 SQL 中:
`
SELECT
user_dim.app_info.app_platform as appPlatform,
event_dim.date as eventDate,
COUNT(event_dim.name) as purchasers,
user_dim.app_info.app_instance_id as appInstanceID,
event_dim.params.value.string_value as contentID,
event_dim.value_in_usd as revenue,
CASE
WHEN event_dim.params.value.string_value = 'content id1' THEN 'content name1'
WHEN event_dim.params.value.string_value = 'content id2' THEN 'content name2'
ELSE 'Undefined'
END as contentName
FROM
TABLE_QUERY([table_ios], 'table_id CONTAINS "app_events_"'),
TABLE_QUERY([table_android], 'table_id CONTAINS "app_events_"')
WHERE`enter code here`
(event_dim.name = 'in_app_purchase' AND event_dim.params.key = 'product_id' OR event_dim.params.key = 'price')
AND event_dim.params.value.string_value != 'null'
GROUP BY
appPlatform, eventDate, appInstanceID, contentID, revenue, contentName
ORDER BY
appPlatform, eventDate DESC
`
【讨论】:
以上是关于无法按 BigQuery/Firebase 中的内容对购买者进行排序的主要内容,如果未能解决你的问题,请参考以下文章
使用 BigQuery Firebase 数据在用户执行 event_name = 'app_remove' 之前查询事件