题解 | #各个视频的平均完播率#
各个视频的平均完播率
https://www.nowcoder.com/practice/96263162f69a48df9d84a93c71045753
看各位佬发的题解,代码都挺短的。我就发一个比较长的
select
video_id,
round(count(if_fi) / count(*), 3) as avg_comp_play_rate
#count()不计入Null值,我在嵌套的子查询中,将没有完播的列的 if_fi 设置为null,就不计入计数了
from
(
select
uv.video_id as video_id,
case
when timestampdiff (SECOND, start_time, end_time) >= duration then 1
else null
end as if_fi
#这里我把tb_user_video_log每一行加了一列 if_fi 表示是否完播
from
tb_user_video_log uv
join tb_video_info v on uv.video_id = v.video_id
where year(start_time) = '2021' #直接过滤掉不是2021年的数据
) as t1
group by
video_id
order by
avg_comp_play_rate desc;
感觉还能优化,奈何我sql优化实在学的不好,希望大佬能够优化一下。

