题解 | #各个视频的平均完播率#
各个视频的平均完播率
https://www.nowcoder.com/practice/96263162f69a48df9d84a93c71045753
select t1.video_id, ROUND(AVG(IF( TIMESTAMPDIFF(SECOND, t1.start_time, t1.end_time) >= t2.duration, 1, 0 )), 3) AS avg_comp_play_rate from tb_user_video_log t1 join tb_video_info t2 on t1.video_id=t2.video_id where year(t1.start_time) =2021 group by t1.video_id order by avg_comp_play_rate desc
解题思路:
- 先看两张表是靠什么连接的-->video_id,这个时候就有两种方法把表连接起来,一种是使用from多表查询,另一种是使用join的连接.
#from多表查询 select video_id from table1 as t1, table2 as t2 where t1.video=t2.video #join连接(数据量大的时候用这个好) select video_id from table1 as t1 join table2 as t2 on t1.video=t2.video
- 筛选条件是什么?排序条件是什么?要不要聚合?
- select的东西怎么写.
学到的新知识点:
- timestampdiff(type,start,end) 返回start和end的差值,以type为条件.
- avg(if(a,1,0)) 这样组合可以求比率,因为avg是对这列所有的数值求和.