元保数科手撕代码
insert into a
values
(1,'2020-09-01',1,0),
(1,'2020-09-02',1,10),
(2,'2020-09-01',2,0),
(2,'2020-09-01',2,0),
(2,'2020-09-01',3,0),
(2,'2020-09-01',3,10);
select sum(case when a2.cons>0 then 1 else 0 end)/count(distinct a1.user_id)
from (select *,row_number()over(partition by user_id order by call_time)as ranking
from a)as a1
inner join (select *,row_number()over(partition by user_id order by call_time)as ranking
from a)as a2
on a1.ranking+1=a2.ranking and a1.user_id=a2.user_id
where a1.ranking=1 and a1.cons=0
insert into a
values
(1,'2020-09-01',1,0),
(1,'2020-09-02',1,10),
(2,'2020-09-01',2,0),
(2,'2020-09-01',2,0),
(2,'2020-09-01',3,0),
(2,'2020-09-01',3,10);
select sum(case when a2.cons>0 then 1 else 0 end)/count(distinct a1.user_id)
from (select *,row_number()over(partition by user_id order by call_time)as ranking
from a)as a1
inner join (select *,row_number()over(partition by user_id order by call_time)as ranking
from a)as a2
on a1.ranking+1=a2.ranking and a1.user_id=a2.user_id
where a1.ranking=1 and a1.cons=0
全部评论
但是注意的是,where ranking=1一定要放在子查询的外面,因为查询顺序是先where 后select
牛客网上很常规的题目变形
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享

点赞 评论 收藏
分享