在BigQuery

时间:2017-04-18 20:51:07

标签: google-bigquery

我正在寻找在bigquery上滚动每周/每月活跃用户的信息。我已尝试过以前的帖子,但使用CROSS JOIN超出了bigQuery的限制。

我使用以下内容作为所需输出的一个非常基本的形式,但我需要一个类似的输出日常而不仅仅是月末日期。

SELECT 
  EXACT_COUNT_DISTINCT(id) AS uniqueInstalls,
  STRFTIME_UTC_USEC(date, '%Y-%m') AS calendarYM
FROM Analytics.EventsTable2 
GROUP BY calendarYM

任何帮助将不胜感激!

干杯!

1 个答案:

答案 0 :(得分:3)

  

我每天需要类似的输出

尝试下面的/使用BigQuery Standard SQL

  
#standardSQL
WITH calendar AS (
  SELECT day 
  FROM UNNEST(GENERATE_DATE_ARRAY(
    (SELECT MIN(DATE) FROM `Analytics.EventsTable2`), 
    (SELECT MAX(DATE) FROM `Analytics.EventsTable2`), 
    INTERVAL 1 DAY)
  ) AS day
)
SELECT 
  c.day AS day,
  COUNT(DISTINCT id) AS uniqueInstalls
FROM calendar AS c
JOIN `Analytics.EventsTable2` AS t
ON t.date BETWEEN DATE_TRUNC(c.day, MONTH) AND c.day
GROUP BY day
ORDER BY day

您可以使用虚拟数据在上面测试/播放,如下所示

#standardSQL
WITH yourTable AS (
  SELECT 1 AS id, DATE("2017-04-01") AS DATE UNION ALL
  SELECT 1, DATE("2017-04-02") UNION ALL
  SELECT 2, DATE("2017-04-02") UNION ALL
  SELECT 1, DATE("2017-04-03") UNION ALL
  SELECT 1, DATE("2017-04-04") UNION ALL
  SELECT 2, DATE("2017-04-04") UNION ALL
  SELECT 3, DATE("2017-04-04") UNION ALL
  SELECT 4, DATE("2017-04-05") UNION ALL
  SELECT 1, DATE("2017-03-02") UNION ALL
  SELECT 2, DATE("2017-03-02") UNION ALL
  SELECT 1, DATE("2017-03-03") UNION ALL
  SELECT 1, DATE("2017-03-04") UNION ALL
  SELECT 2, DATE("2017-03-04") UNION ALL
  SELECT 3, DATE("2017-03-04") UNION ALL
  SELECT 4, DATE("2017-03-05") 
),
calendar AS (
  SELECT day 
  FROM UNNEST(GENERATE_DATE_ARRAY(
    (SELECT MIN(DATE) FROM yourTable), 
    (SELECT MAX(DATE) FROM yourTable), 
    INTERVAL 1 DAY)
  ) AS day
)
SELECT 
  c.day AS day,
  COUNT(DISTINCT id) AS uniqueInstalls
FROM calendar AS c
JOIN yourTable AS t
ON t.date BETWEEN DATE_TRUNC(c.day, MONTH) AND c.day
GROUP BY day
ORDER BY day  
  

第一个查询返回以下错误错误:函数GENERATE_DATE_ARRAY没有匹配的签名

以下是输入数据中TIMESTAMP的版本

#standardSQL
WITH calendar AS (
  SELECT day 
  FROM UNNEST(GENERATE_DATE_ARRAY(
    (SELECT MIN(DATE(date)) FROM `Analytics.EventsTable2`), 
    (SELECT MAX(DATE(date)) FROM `Analytics.EventsTable2`), 
    INTERVAL 1 DAY)
  ) AS day
)
SELECT 
  c.day AS day,
  COUNT(DISTINCT id) AS uniqueInstalls
FROM calendar AS c
JOIN `Analytics.EventsTable2` AS t
ON DATE(t.date) BETWEEN DATE_TRUNC(c.day, MONTH) AND c.day
GROUP BY day
ORDER BY day