在 SQL 中滚动新活跃用户 (BigQuery)

Rolling NEW active users in SQL (BigQuery)

我已经计算了滚动活跃用户(每周)如下:

    SELECT
      DATE_TRUNC(EXTRACT(DATE FROM tracks.timestamp), WEEK),
      COUNT(DISTINCT tracks.user_id)
    FROM `company.dataset.tracks` AS tracks
    WHERE tracks.timestamp > TIMESTAMP('2020-01-01')
    AND tracks.event = 'activation_event'
    GROUP BY 1
    ORDER BY 1

我想知道每周第一次执行激活事件的不同用户的数量。

如果我没听错的话,你可以使用两个级别的聚合:

select 
    date_trunc(date(activation_timestamp), week) activation_week, 
    count(*) cnt_active_users
from (
    select min(timestamp) activation_timestamp
    from `company.dataset.tracks` t
    where event = 'activation_event'
    group by user_id
) t
where activation_timestamp > timestamp('2020-01-01

子查询计算每个用户第一次激活事件的日期,然后外部查询计算每周此类事件的数量。

如果您希望在同一个查询中同时进行活动和开始:

SELECT week, COUNT(*) as users_in_week,
       COUNTIF(seqnum = 1) as new_users
FROM (SELECT DATE_TRUNC(EXTRACT(DATE FROM t.timestamp), WEEK) as week,
             t.user_id, COUNT(*) as cnt,
             ROW_NUMBER() OVER (PARTITION BY t.user_id ORDER BY MIN(t.timestamp)) as seqnum
      FROM `company.dataset.tracks` t
      WHERE t.event = 'activation_event'
      GROUP BY 1, 2
     ) t
WHERE tracks.timestamp > TIMESTAMP('2020-01-01')
GROUP BY 1
ORDER BY 1;