如何获得每月7天的活跃用户?

时间:2017-01-19 18:21:50

标签: sql firebase google-bigquery firebase-analytics

在我的数据库中,我有两个字段用于标识用户timestampinstance_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 个答案:

答案 0 :(得分:2)

尝试以下

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