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

各个视频的平均完播率

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是对这列所有的数值求和.
全部评论

相关推荐

今天 11:42
江西农业大学 C++
点赞 评论 收藏
分享
05-16 11:16
已编辑
东华理工大学 Java
牛客73769814...:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
牛客创作赏金赛
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-02 15:39
希望奇迹发生的布莱克...:真的是 现在卷实习就是没苦硬吃
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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