24小时留存计算

表名为 src_table, 表中只有两列,玩家ID和游戏时间戳 user_id, event_timestamp, 求玩家24小时的留存。从新增开始每24小时算一天。

user_idevent_timestamp
a2022-08-01T00:01:00Z
a2022-08-01T00:05:00Z
b2022-08-01T00:06:00Z
with t1 as (
  -- 去重
  select distinct a.user_id, event_timestamp
  from src_table a
), t2 as (
  -- 求出首次出现时间
  select user_id, min(event_timestamp) as first_ts
  from src_table
  group by 1
), t3 as (
  select distinct a.user_id, date(first_ts) as first_date, TIMESTAMP_DIFF(event_timestamp, first_ts, day) as day
  from t1 a
  inner join t2 b
  on a.user_id = b.user_id
)
 select first_date, count(case when day = 0 then 1 end) as new_users,
  count(case when day = 1 then 1 end) as day2_active_users,
  count(case when day = 2 then 1 end) as day3_active_users,
  count(case when day = 6 then 1 end) as day7_active_users,
from `t3`
group by 1
order by 1
;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值