使用BigQuery计算当天7天活跃用户?

时间:2017-04-07 17:28:05

标签: sql google-bigquery

如果我没有弄错,要计算当前活跃用户应该很简单。简单地说今天和x天回来(7天活动将是6回)然后计算不同的ID。我对2天的活跃用户有以下查询:

WITH allTables AS (
  SELECT 
    CONCAT(user_dim.app_info.app_id, ':', user_dim.app_info.app_platform) AS app,
    event.date,
    user_dim.app_info.app_instance_id as users
  FROM `dataset.app_events_intraday_20170407`
  CROSS JOIN
    UNNEST(event_dim) AS event

  UNION ALL
  SELECT 
    CONCAT(user_dim.app_info.app_id, ':', user_dim.app_info.app_platform) AS app,
    event.date,
    user_dim.app_info.app_instance_id as users
  FROM `dataset.app_events_20170406`
  CROSS JOIN
    UNNEST(event_dim) AS event
) SELECT COUNT(DISTINCT(users)) AS unique,
   COUNT(users) as total
FROM allTables

这是为期2天的活动,但是对于7天或30天,我只会将所有这些表联合起来。这是正确的还是需要修改?

1 个答案:

答案 0 :(得分:2)

您应该尝试使用Querying Multiple Tables Using a Wildcard Table

,而不是使用UNION ALL

尝试以下内容

  
#standardSQL
WITH allTables AS (
  SELECT 
    CONCAT(user_dim.app_info.app_instance_id, ':', user_dim.app_info.app_platform) AS app,
    event.date,
    user_dim.app_info.app_instance_id AS users
  FROM `dataset.app_events_intraday_*`, UNNEST(event_dim) AS event
  WHERE _TABLE_SUFFIX BETWEEN '20170401' AND '20170407' 
  UNION ALL
  SELECT 
    CONCAT(user_dim.app_info.app_instance_id, ':', user_dim.app_info.app_platform) AS app,
    event.date,
    user_dim.app_info.app_instance_id AS users
  FROM `dataset.app_events_*`, UNNEST(event_dim) AS event
  WHERE _TABLE_SUFFIX BETWEEN '20170401' AND '20170407' 
) 
SELECT 
  COUNT(DISTINCT(users)) AS unique,
  COUNT(users) AS total
FROM allTables

您可以在下面使用WHERE子句使其更通用

WHERE _TABLE_SUFFIX 
   BETWEEN FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 6 DAY)) 
   AND FORMAT_DATE('%Y%m%d', CURRENT_DATE())

另请注意:我已将app_id中的user_dim.app_info.app_id更改为app_instance_id,因为我认为这是您的错误 - 但我可能是错的