【场景】:平均

【分类】:日期函数、timestampdiff、select if

分析思路

难点:

1.判断是否完整播放视频,用到日期函数

做这种题的时候,如果要用到2个count,分别测试count列的值,再做除法

判断是否是2021年的播放记录;判断是否完整播放视频

  • [条件]:if(timestampdiff(second,start_time,end_time)>= duration,1,null);where year(start_time) = 2021

  • [使用]:group by video_id

求解代码

方法一:

select 
    video_id,
    round(count(if(timestampdiff(second,start_time,end_time)>= duration,1,null))/count(start_time),3) as avg_comp_play_rate
from tb_user_video_log
join tb_video_info
using (video_id)
where year(start_time) = 2021
group by video_id
order by avg_comp_play_rate desc