BigQuery每月活跃用户?

时间:2017-01-19 14:40:55

标签: sql firebase google-bigquery firebase-analytics

我目前正在处理来自此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

1 个答案:

答案 0 :(得分:3)

  

列名称周期在[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