题解 | #各个视频的平均完播率#
各个视频的平均完播率
https://www.nowcoder.com/practice/96263162f69a48df9d84a93c71045753
#1、 注意 timestampdiff与datediff的区别
# datediff函数,返回值是相差的天数,不能定位到小时、分钟和秒
# timestampdiff函数,有参数设置,可以精确到天(DAY)、小时(HOUR),分钟(MINUTE)和秒(SECOND)时间小的放在前面,时间大的放在后面。timestampdiff(SECOND, '2022-01-03 00:00:00', '2022-01-04 02:00:00')
# 2、要取出2021年的数据
# 3、要降序排序
select video_id, round(sum(case when timestampdiff(second, start_time, end_time) >= duration then 1 else 0 end)/count(*), 3) avg_comp_play_rate
from(
select tuvl.video_id, tuvl.end_time, tuvl.start_time, tvi.duration
from tb_user_video_log tuvl left join tb_video_info tvi on tuvl.video_id = tvi.video_id where year(start_time)=2021
) t group by video_id order by avg_comp_play_rate desc