留存率,越来越受到大家的关注,从网站用户到客户端产品,游戏产品,无线APP产品,都非常重视这一指标,留存率成为衡量一个产品是否健康成长的重要指标之一。
SELECT
first_day,
sum(case when by_day = 0 then 1 else 0 end) day_0,
sum(case when by_day = 1 then 1 else 0 end)/sum(case when by_day = 0 then 1 else 0 end) day_1_rate,
sum(case when by_day = 3 then 1 else 0 end)/sum(case when by_day = 0 then 1 else 0 end) day_3_rate,
sum(case when by_day = 7 then 1 else 0 end)/sum(case when by_day = 0 then 1 else 0 end) day_7_rate,
sum(case when by_day = 14 then 1 else 0 end)/sum(case when by_day = 0 then 1 else 0 end) day_14_rate,
sum(case when by_day = 30 then 1 else 0 end)/sum(case when by_day = 0 then 1 else 0 end) day_30_rate
FROM
(SELECT
user_id,
login_time,
first_day,
DATEDIFF(login_time,first_day) as by_day
FROM
(SELECT
b.user_id,
b.login_time,
c.first_day
FROM
(SELECT
user_id,
str_to_date(login_time,'%Y-%m-%d') login_time
FROM user_info
GROUP BY 1,2) b
LEFT JOIN
(SELECT
user_id,
min(login_time) first_day
FROM
(select
user_id,
str_to_date(login_time,'%Y-%m-%d') login_time
FROM
user_info
group by 1,2) a
group by 1) c
on b.user_id = c.user_id
order by 1,2) e
order by 1,2) f
group by 1
order by 1
with demo_temp as(
SELECT
user_id,
str_to_date(login_time,'%Y-%m-%d') login_time
FROM user_info
GROUP BY 1,2
)
SELECT
first_day,
sum(case when by_day = 0 then 1 else 0 end) day_0,
concat(truncate(sum(case when by_day = 1 then 1 else 0 end)/sum(case when by_day = 0 then 1 else 0 end) *100,2),'%') day_1_rate,
concat(truncate(sum(case when by_day = 3 then 1 else 0 end)/sum(case when by_day = 0 then 1 else 0 end) *100,2),'%') day_3_rate,
concat(truncate(sum(case when by_day = 7 then 1 else 0 end)/sum(case when by_day = 0 then 1 else 0 end) *100,2),'%') day_7_rate,
concat(truncate(sum(case when by_day = 14 then 1 else 0 end)/sum(case when by_day = 0 then 1 else 0 end) *100,2),'%') day_14_rate,
concat(truncate(sum(case when by_day = 30 then 1 else 0 end)/sum(case when by_day = 0 then 1 else 0 end) *100,2),'%') day_30_rate
FROM
(SELECT
user_id,
login_time,
first_day,
DATEDIFF(login_time,first_day) as by_day
FROM
(SELECT
b.user_id,
b.login_time,
c.first_day
FROM
demo_temp b
LEFT JOIN
(SELECT
user_id,
min(login_time) first_day
FROM demo_temp a
group by 1) c
on b.user_id = c.user_id
order by 1,2) e
order by 1,2) f
group by 1
order by 1
https://blog.csdn.net/joeyon1985/article/details/49022913
Hive _ 统计日活,留存等指标 设计与实现 指南
https://blog.csdn.net/u010003835/article/details/89212558
https://blog.csdn.net/MsSpark/article/details/86727058 阿里云
https://www.sogou.com/link?url=hedJjaC291OB0PrGj_c3jJzmXqp0xreS5db_orznGCwicbqryMhLhibc-iPH6wf8XRWodySSjrZ5gPVahMZ6-A..