相关sql题汇总

计算cohort留存方法

非连续活跃为分子

select total.first_day,COUNT(distinct total.user_id),
count(distinct (case when total.gap=0 then total.user_id else null end )),
count(distinct (case when total.gap=1 then total.user_id else null end )),
count(distinct (case when total.gap=2 then total.user_id else null end )),
count(distinct (case when total.gap=3 then total.user_id else null end )),
count(distinct (case when total.gap=4 then total.user_id else null end ))
from 
(
select a.*,b.first_day,DATEDIFF(d,b.first_day,a.p_date)as gap
from 
(select p_date,user_id
from dbo.a 
where p_date>='2020-01-01' and p_date<='2020-12-31'
group by p_date,user_id
)a
left join
(select user_id,min(p_date)as first_day
from dbo.a 
where p_date>='2020-01-01' and p_date<='2020-12-31'
group by user_id
)b
on a.user_id=b.user_id 
where DATEDIFF(d,b.first_day,a.p_date)<=5
)total
group by total.first_day

连续活跃为分子

select total_1.first_day,COUNT(total_1.user_id),
sum(total_1.cnt_1),
sum(total_1.cnt_2),
sum(total_1.cnt_3),
sum(total_1.cnt_4),
sum(total_1.cnt_5)
from 
(select total.first_day,total.user_id,
(case when count( (case when total.gap=0 then total.user_id else null end )) =1 then 1 else 0 end) as cnt_1,
(case when count( (case when total.gap<=1 then total.user_id else null end)) =2 then 1 else 0 end) as cnt_2,
(case when count( (case when total.gap<=2 then total.user_id else null end ))=3 then 1 else 0 end) as cnt_3,
(case when count( (case when total.gap<=3 then total.user_id else null end ))=4 then 1 else 0 end) as cnt_4,
(case when count( (case when total.gap<=4 then total.user_id else null end ))=5 then 1 else 0 end) as cnt_5
from 
(
select a.*,b.first_day,DATEDIFF(d,b.first_day,a.p_date)as gap
from 
(select p_date,user_id
from dbo.a 
where p_date>='2020-01-01' and p_date<='2020-12-31'
group by p_date,user_id
)a
left join
(select user_id,min(p_date)as first_day
from dbo.a 
where p_date>='2020-01-01' and p_date<='2020-12-31'
group by user_id
)b
on a.user_id=b.user_id 
where DATEDIFF(d,b.first_day,a.p_date)<=5
)total
group by total.first_day,total.user_id
)total_1
group by total_1.first_day

使用lag

在这里插入图片描述

select a.user_id,avg(DATEDIFF(n,num,time))
from 
(select user_id,stats,time,LAG(time,1,null) over(partition by user_id order by time) as num
from test_kuai)a
where stats='Leave'
group by a.user_id

分组排序

select a.*
from
(
select *,rank()over(partition by Grade order by Score asc) as num
from Student
)a
where a.num<=2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值