SQL156. 各个视频的平均完播率
描述
id | uid | video_id | start_time | end_time | if_follow | if_like | if_retweet | comment_id |
1 | 101 | 2001 | 2021-10-01 10:00:00 | 2021-10-01 10:00:30 | 0 | 1 | 1 | NULL |
2 | 102 | 2001 | 2021-10-01 10:00:00 | 2021-10-01 10:00:24 | 0 | 0 | 1 | NULL |
3 | 103 | 2001 | 2021-10-01 11:00:00 | 2021-10-01 11:00:34 | 0 | 1 | 0 | 1732526 |
4 | 101 | 2002 | 2021-09-01 10:00:00 | 2021-9-01 10:00:42 | 1 | 0 | 1 | NULL |
5 | 102 | 2002 | 2021-10-01 11:00:00 | 2021-10-01 10:00:30 | 1 | 0 | 1 | NULL |
id | video_id | author | tag | duration | release_time |
1 | 2001 | 901 | 影视 | 30 | 2021-01-01 07:00:00 |
2 | 2002 | 901 | 美食 | 60 | 2021-01-01 07:00:00 |
3 | 2003 | 902 | 旅游 | 90 | 2021-01-01 07:00:00 |
video_id | avg_comp_play_rate |
2001 | 0.667 |
2002 | 0.000 |
示例1
输入:
DROP TABLE IF EXISTS tb_user_video_log, tb_video_info; CREATE TABLE tb_user_video_log ( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', uid INT NOT NULL COMMENT '用户ID', video_id INT NOT NULL COMMENT '视频ID', start_time datetime COMMENT '开始观看时间', end_time datetime COMMENT '结束观看时间', if_follow TINYINT COMMENT '是否关注', if_like TINYINT COMMENT '是否点赞', if_retweet TINYINT COMMENT '是否转发', comment_id INT COMMENT '评论ID' ) CHARACTER SET utf8 COLLATE utf8_bin; CREATE TABLE tb_video_info ( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', video_id INT UNIQUE NOT NULL COMMENT '视频ID', author INT NOT NULL COMMENT '创作者ID', tag VARCHAR(16) NOT NULL COMMENT '类别标签', duration INT NOT NULL COMMENT '视频时长(秒数)', release_time datetime NOT NULL COMMENT '发布时间' )CHARACTER SET utf8 COLLATE utf8_bin; INSERT INTO tb_user_video_log(uid, video_id, start_time, end_time, if_follow, if_like, if_retweet, comment_id) VALUES (101, 2001, '2021-10-01 10:00:00', '2021-10-01 10:00:30', 0, 1, 1, null), (102, 2001, '2021-10-01 10:00:00', '2021-10-01 10:00:24', 0, 0, 1, null), (103, 2001, '2021-10-01 11:00:00', '2021-10-01 11:00:34', 0, 1, 0, 1732526), (101, 2002, '2021-09-01 10:00:00', '2021-09-01 10:00:42', 1, 0, 1, null), (102, 2002, '2021-10-01 11:00:00', '2021-10-01 11:00:30', 1, 0, 1, null); INSERT INTO tb_video_info(video_id, author, tag, duration, release_time) VALUES (2001, 901, '影视', 30, '2021-01-01 7:00:00'), (2002, 901, '美食', 60, '2021-01-01 7:00:00'), (2003, 902, '旅游', 90, '2021-01-01 7:00:00');
输出:
2001|0.667 2002|0.000
Mysql 解法, 执行用时: 36ms, 内存消耗: 6380KB, 提交时间: 2021-12-27
select a.video_id, round(sum(case when timestampdiff(second,b.start_time,b.end_time)>=a.duration then 1 else 0 end)/count(b.uid),3) as avg_com_play_rate from tb_video_info a join tb_user_video_log b on a.video_id=b.video_id where YEAR(b.start_time) = 2021 group by a.video_id order by avg_com_play_rate desc
Mysql 解法, 执行用时: 36ms, 内存消耗: 6388KB, 提交时间: 2022-01-25
select a.video_id, round(avg(if(timestampdiff(second,start_time,end_time)>=duration,1,0)),3) as avg_comp_play_rate from tb_user_video_log a left join tb_video_info b on a.video_id = b.video_id where year(start_time)=2021 group by a.video_id order by avg_comp_play_rate DESC;
Mysql 解法, 执行用时: 36ms, 内存消耗: 6388KB, 提交时间: 2021-12-14
select log.video_id, round(sum(if(TIMESTAMPDIFF(second,log.start_time,log.end_time) >=info.duration,1,0))/count(log.video_id) ,3) as avg_comp_play_rate from tb_user_video_log as log inner join tb_video_info as info on log.video_id=info.video_id where year(log.start_time)=2021 group by video_id order by avg_comp_play_rate desc
Mysql 解法, 执行用时: 36ms, 内存消耗: 6392KB, 提交时间: 2021-12-14
select t1.video_id, round(avg(if(TIMESTAMPDIFF(second, start_time, end_time) >= t2.duration, 1, 0)),3) avg_comp_play_rate from tb_user_video_log t1 join tb_video_info t2 on t1.video_id = t2.video_id where YEAR(start_time) = 2021 group by t1.video_id order by avg_comp_play_rate desc
Mysql 解法, 执行用时: 36ms, 内存消耗: 6392KB, 提交时间: 2021-12-12
SELECT a.video_id, round(sum(if((end_time-start_time-b.duration) >=0,1,0))/count(a.video_id),3) avg_comp_play_rate FROM tb_user_video_log a left join tb_video_info b on a.video_id = b.video_id where year(start_time) = 2021 GROUP BY a.video_id order by avg_comp_play_rate desc