题解 | #各个视频的平均完播率#

各个视频的平均完播率

https://www.nowcoder.com/practice/96263162f69a48df9d84a93c71045753

1.先求完播次数

select
    tuvl.video_id,
    count(*) finish_cnt
from
    tb_user_video_log tuvl,
    tb_video_info tvi
where tuvl.video_id = tvi.video_id
and year(start_time) = 2021
and timediff(end_time, start_time) >= tvi.duration
group by tuvl.video_id

2.再求全部播放次数

select
    video_id,
    count(*) view_cnt
from
    tb_user_video_log tuvl
where year(tuvl.start_time) = 2021
group by tuvl.video_id

3. 求完播率

select
    t2.video_id,
    format(ifnull(finish_cnt, 0)/view_cnt, 3) avg_comp_play_rate
from
    (
        select
            tuvl.video_id,
            count(*) finish_cnt
        from
            tb_user_video_log tuvl,
            tb_video_info tvi
        where tuvl.video_id = tvi.video_id
        and year(start_time) = 2021
        and timediff(end_time, start_time) >= tvi.duration
        group by tuvl.video_id
    )t1 right join
    (
        select
            video_id,
            count(*) view_cnt
        from
            tb_user_video_log tuvl
        where year(tuvl.start_time) = 2021
        group by tuvl.video_id
    )t2
on t1.video_id = t2.video_id
order by avg_comp_play_rate desc

4. 由于1、2步骤中条件很相似,可以进行优化合并在一起

select
    tuvl.video_id,
    format(sum(if(timediff(end_time, start_time) >= tvi.duration, 1,0))/count(*), 3) avg_comp_play_rate
from
    tb_user_video_log tuvl,
    tb_video_info tvi
where tuvl.video_id = tvi.video_id
and year(start_time) = 2021 
group by tuvl.video_id
order by avg_comp_play_rate desc

全部评论

相关推荐

04-22 15:13
已编辑
Java
点赞 评论 收藏
分享
野猪不是猪🐗:现在的环境就是这样,供远大于求。 以前卡学历,现在最高学历不够卡了,还要卡第一学历。 还是不够筛,于是还要求得有实习、不能有gap等等... 可能这个岗位总共就一个hc,筛到最后还是有十几个人满足这些要求。他们都非常优秀,各方面都很棒。 那没办法了,看那个顺眼选哪个呗。 很残酷,也很现实
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务