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

各个视频的平均完播率

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

全部评论

相关推荐

头像
07-01 18:39
已编辑
门头沟学院 Java
白火同学:我刚出学校那会沟通过最逆天的公司是一家初创公司,老板亲自在BOSS上当HR,说初创公司人没多少,前期比较艰苦,运维实施前后端都得干。问工资有多少,答两千,给技术入股。我寻思我饭都吃不起了,你跟我谈股份。
我的求职精神状态
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务