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

各个视频的平均完播率

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

全部评论

相关推荐

05-09 14:45
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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