php mysql计算留存率,【hive】关于用户留存率的计算

首先用户留存率一般是面向新增用户的概念,是指某一天注册后的几天还是否活跃,是以每天为单位进行计算的.

一般收到的需求都是一个时间段内的新增用户的几天留存

(1)找到这个时间段内的新增用户(也可能含有地区啊的各种附加条件),一般在日活表中有记录是否是新增状态.

注意,需要以天为单位进行分组找出用户的id.因为留存率都是以每天为单位进行计算的.

表结构(register_date,user_id)

(2)找到这个时间段内的活跃用户(active_date,user_id)

(3)以 1表 为主表left join 2表 以user_id为关联键,统计留存数

这样后的记录类型为:register_date,user_id,active_date

register_date为新增日期,即留存率的单位天.

user_id为用户id,distinct user_id来计算用户数

留存率怎么算?

active_date - register_date = 1,说明注册的次日用户是活跃的,所以count+1

所以我们只要关注 active_date 和 register_date 相差天数即可统计留存数

取天数差的时候用datediff(active_date,register_date)来计算,active_date 和 register_date 的格式为 yyyy-MM-dd

(4)计算留存率

模板:

SET mapreduce.job.queuename=xxx;

SET mapreduce.job.name=xxx;set mapreduce.job.reduces=19;select '日期', '注册用户数', '次日留存率', '2日留存率', '3日留存率', dim_date

,total_cnt

,concat_ws('% |', cast(round(dif_1cnt*100/total_cnt, 2) as string), cast(dif_1cnt as string))

,concat_ws('% |', cast(round(dif_2cnt*100/total_cnt, 2) as string), cast(dif_2cnt as string))

,concat_ws('% |', cast(round(dif_3cnt*100/total_cnt, 2) as string), cast(dif_3cnt as string))

,concat_ws('% |', cast(round(dif_4cnt*100/total_cnt, 2) as string), cast(dif_4cnt as string))from(selectp1.state dim_date

,p1.device_os

,count(distinct p1.user_id) total_cnt

,count(distinctif(datediff(p3.state,p1.state) = 1, p1.user_id, null)) dif_1cnt

,count(distinctif(datediff(p3.state,p1.state) = 2, p1.user_id, null)) dif_2cnt

,count(distinctif(datediff(p3.state,p1.state) = 3, p1.user_id, null)) dif_3cnt

,count(distinctif(datediff(p3.state,p1.state) = 4, p1.user_id, null)) dif_4cntfrom(selectfrom_unixtime(unix_timestamp(cast(partition_dateas string), 'yyyyMMdd'), 'yyyy-MM-dd') state,

user_idfromuser_active_daywherepartition_date between date1 and date2

and user_is_new= 1group by1,2)p1--日新增用户名单(register_date,user_id)

left outer join

(selectfrom_unixtime(unix_timestamp(cast(partition_dateas string), 'yyyyMMdd'), 'yyyy-MM-dd') state,

user_idfromactive_userswherepartition_date between date1 and date2

group by1,2)p3--期间活跃用户(active_date,user_id)

on (p3.user_id=p1.user_id)

group by1,2) p4;

友情附赠:

求用户首充的时间,在充值表中以user_id分组,然后找出最小的订单添加时间,对应的订单就是首充的记录.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值