BigQuery 月度活跃用户?
Posted
技术标签:
【中文标题】BigQuery 月度活跃用户?【英文标题】:BigQuery Monthly Active Users? 【发布时间】:2017-01-19 14:40:54 【问题描述】:我目前正在处理来自此post 的查询。该查询是用旧版 SQL 编写的,在我的环境中不起作用。我已修改查询以使用现代 SQL 函数,并将 SELECT date as date
更新为使用 timestamp_micros
。
我还应该提到,我尝试选择的行来自 Firebase Analytics。
我的查询:
SELECT
FORMAT_TIMESTAMP('%Y-%m-%d', TIMESTAMP_MICROS(event.timestamp_micros)) as date,
SUM(CASE WHEN period = 7 THEN users END) as days_07,
SUM(CASE WHEN period = 14 THEN users END) as days_14,
SUM(CASE WHEN period = 30 THEN users END) as days_30
FROM (
SELECT
FORMAT_TIMESTAMP('%Y-%m-%d', TIMESTAMP_MICROS(event.timestamp_micros)) as date,
periods.period as period,
COUNT(DISTINCT user_dim.app_info.app_instance_id) as users
FROM `com_sidearm_fanapp_uiowa_ios.*` as activity
CROSS JOIN
UNNEST(event_dim) as event
CROSS JOIN (
SELECT
FORMAT_TIMESTAMP('%Y-%m-%d', TIMESTAMP_MICROS(event.timestamp_micros)) as date
FROM `com_sidearm_fanapp_uiowa_IOS.*`
CROSS JOIN
UNNEST(event_dim) as event
GROUP BY event.timestamp_micros
) as dates
CROSS JOIN (
SELECT
period
FROM
(
SELECT 7 as period
UNION ALL
SELECT 14 as period
UNION ALL
SELECT 30 as period
)
) as periods
WHERE
dates.date >= activity.date
AND
SAFE_CAST(FLOOR(TIMESTAMP_DIFF(dates.date, activity.date, DAY)/periods.period) AS INT64) = 0
GROUP BY 1,2
)
CROSS JOIN
UNNEST(event_dim) as event
GROUP BY date
ORDER BY date DESC
【问题讨论】:
对于其他访问这篇文章的人,BQ 导出模式定义为here。 【参考方案1】:在 [24:13] 错误时列名句点不明确。
要修复这个特定的错误 - 你应该在下面修复
CROSS JOIN (
SELECT
period
FROM
(SELECT 7 as period),
(SELECT 14 as period),
(SELECT 30 as period)
) as periods
所以它应该看起来像:
CROSS JOIN (
SELECT
period
FROM
(SELECT 7 as period UNION ALL
SELECT 14 as period UNION ALL
SELECT 30 as period)
) as periods
回答您更新的问题
试试下面。我没有机会测试它,但希望它可以帮助您解决您的查询
SELECT
date,
SUM(CASE WHEN period = 7 THEN users END) as days_07,
SUM(CASE WHEN period = 14 THEN users END) as days_14,
SUM(CASE WHEN period = 30 THEN users END) as days_30
FROM (
SELECT
activity.date as date,
periods.period as period,
COUNT(DISTINCT user) as users
FROM (
SELECT
event.timestamp_micros as date,
user_dim.app_info.app_instance_id as user
FROM `yourTable` CROSS JOIN UNNEST(event_dim) as event
) as activity
CROSS JOIN (
SELECT
event.timestamp_micros as date
FROM `yourTable` CROSS JOIN UNNEST(event_dim) as event
GROUP BY event.timestamp_micros
) as dates
CROSS JOIN (
SELECT period
FROM
(SELECT 7 as period UNION ALL
SELECT 14 as period UNION ALL
SELECT 30 as period)
) as periods
WHERE dates.date >= activity.date
AND SAFE_CAST(FLOOR(TIMESTAMP_DIFF(TIMESTAMP_MICROS(dates.date), TIMESTAMP_MICROS(activity.date), DAY)/periods.period) AS INT64) = 0
GROUP BY 1,2
)
GROUP BY date
ORDER BY date DESC
【讨论】:
这是有效的,但现在又抛出了另一个错误:Name date not found inside activity at [31:32]
正如您在问题中提到的 - 原始查询是为其中包含 date
字段的不同表编写的。所以你需要确保你使用的文件存在于你的表中
我查询的字段确实存在于我的表中。我很确定这一点。我已将date
更改为timestamp_micros
请查看更新后的问题。我更改了 date
字段的格式以匹配原始旧 SQL 查询中的内容。
@JoeScotto - 这不是 SO 的工作方式 - 如果您有新问题,您应该将其作为新问题发布。相反,您不断更改现有问题,从而使原始问题的答案完全无效!作为一个例外(对我自己而言),我会再回答一次——但在未来——我不会回答这种永无止境的一体式问题。顺便说一句——通常你的问题的标题不应该是你正在处理的项目或任务名称——而是你遇到的明确的简短和具体的问题。我知道这并不总是那么简单 - 但这就是 SO 的工作原理以上是关于BigQuery 月度活跃用户?的主要内容,如果未能解决你的问题,请参考以下文章