如何获取每月 7 天活跃用户?
Posted
技术标签:
【中文标题】如何获取每月 7 天活跃用户?【英文标题】:How to get the monthly 7-day active users? 【发布时间】:2017-01-19 18:21:50 【问题描述】:在我的数据库中,我有两个字段用于识别用户,timestamp
和 instance_id
。我希望能够从这些数据中获取每月 7 天的活跃用户。我尝试了以下查询,但它只为每一行返回相同的时间戳和 1。
SELECT
FORMAT_TIMESTAMP('%Y-%m-%d', TIMESTAMP_MICROS(date)) as target,
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 `hidden.*`
CROSS JOIN
UNNEST(event_dim) as event
) as activity
CROSS JOIN (
SELECT
event.timestamp_micros as date
FROM `hidden.*`
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
我不太确定从哪里开始,这对我来说很有挑战性,因为我不是最擅长 SQL。任何帮助都会很棒。谢谢!
我还应该提到,这些查询将在 BigQuery 中运行,并且数据将从 Firebase 导出到 BigQuery。
【问题讨论】:
【参考方案1】:下面试试
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 DISTINCT
DATE(TIMESTAMP_MICROS(event.timestamp_micros)) AS DATE,
user_dim.app_info.app_instance_id AS user
FROM `firebase-analytics-sample-data.android_dataset.app_events_20160607`
CROSS JOIN UNNEST(event_dim) AS event
) AS activity
CROSS JOIN (
SELECT DATE(TIMESTAMP_MICROS(event.timestamp_micros)) AS DATE
FROM `firebase-analytics-sample-data.android_dataset.app_events_20160607`
CROSS JOIN UNNEST(event_dim) AS event
GROUP BY 1
) 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(DATE_DIFF(dates.date, activity.date, DAY)/periods.period) AS INT64) = 0
GROUP BY 1,2
)
GROUP BY DATE
ORDER BY DATE DESC
【讨论】:
效果很好!由于您似乎是这里的主要 BigQuery 人员,您能否快速解释一下计费层级是什么?我已经尝试阅读文档,但它非常令人困惑并且没有多大意义。此查询尤其需要第 8 层。 您可以在High-Compute queries 的文档中阅读有关计费层级的信息。它提供了相当详细的主题图片 谢谢,之前没看到这个链接。 我想我在一个地方错过了“修复”,它仍然在日期子查询中按微量与日期进行分组 - 请尝试让我们知道计费等级是否仍为 8 我删除了 14 天和 30 天的查询,现在它要求我认为还不错的第 3 层。以上是关于如何获取每月 7 天活跃用户?的主要内容,如果未能解决你的问题,请参考以下文章