相关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
发布了23 篇原创文章 · 获赞 0 · 访问量 622

猜你喜欢

转载自blog.csdn.net/macmurphy/article/details/105494766