2017-01-19 18 views
3

私は現在、このpostからのクエリを処理しています。そのクエリはレガシーSQLで書かれており、私の環境では動作しません。最新のSQL関数を使用するようにクエリを変更し、SELECT date as datetimestamp_microsに更新しました。BigQuery Monthly Active Users?

また、私が選択しようとしている行が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

を修正するのに役立つことを願って、BQのエクスポートスキーマが定義されていますfirebase/answer/7029846?hl = ja&ref_topic = 7029512)を参照してください。 – Kato

答えて

3

Column name period is ambiguous at [24:13] error.

この特定のエラーを修正する - あなたはそれがどのように見えるはずです

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 

Answer on your updated question

以下を試してください。私はhttps://support.google.com/([こちら]それをテストする機会を持っていなかったが、それはこの記事を訪問し、他人のために、あなたのクエリ

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 
+0

これは動作していますが、別のエラーが表示されています: '名前の日付が[31:32]のアクティビティ内に見つかりませんでした。 ' –

+0

あなたの質問で言及したように - 元のクエリは、あなたのテーブルに存在するfiledを使用していることを確認する必要があります –

+0

私が照会しているフィールドは私のテーブルにあります。私はこれを確信しています。私は 'date'を' timestamp_micros'に変更しました –