列表

详情


SQL157. 平均播放进度大于60%的视频类别

描述

用户-视频互动表tb_user_video_log
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:21
0 0 1 NULL
3 103
2001 2021-10-01 11:00:50
2021-10-01 11:01:20
0 1 0 1732526
4 102
2002 2021-10-01 11:00:00
2021-10-01 11:00:30
1 0 1 NULL
5 103
2002 2021-10-01 10:59:05
2021-10-01 11:00:05
1 0 1 NULL
uid-用户ID, video_id-视频ID, start_time-开始观看时间, end_time-结束观看时间, if_follow-是否关注, if_like-是否点赞, if_retweet-是否转发, comment_id-评论ID)


短视频信息表tb_video_info
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-视频ID, author-创作者ID, tag-类别标签, duration-视频时长, release_time-发布时间)


问题:计算各类视频的平均播放进度,将进度大于60%的类别输出。

  • 播放进度=播放时长÷视频时长*100%,当播放时长大于视频时长时,播放进度均记为100%。
  • 结果保留两位小数,并按播放进度倒序排序。

输出示例
示例数据的输出结果如下:
tag avg_play_progress
影视
90.00%
美食
75.00%
解释:
影视类视频2001被用户101、102、103看过,播放进度分别为:30秒(100%)、21秒(70%)、30秒(100%),平均播放进度为90.00%(保留两位小数);
美食类视频2002被用户102、103看过,播放进度分别为:30秒(50%)、60秒(100%),平均播放进度为75.00%(保留两位小数)

示例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:21', 0, 0, 1, null),
  (103, 2001, '2021-10-01 11:00:50', '2021-10-01 11:01:20', 0, 1, 0, 1732526),
  (102, 2002, '2021-10-01 11:00:00', '2021-10-01 11:00:30', 1, 0, 1, null),
  (103, 2002, '2021-10-01 10:59:05', '2021-10-01 11:00:05', 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, '2020-01-01 7:00:00');

输出:

影视|90.00%
美食|75.00%

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Mysql 解法, 执行用时: 36ms, 内存消耗: 6388KB, 提交时间: 2022-01-03

select
tag,
concat(
    ROUND(
        avg(
            if(
                timestampdiff(second,start_time,end_time)>=duration,
                1,timestampdiff(second,start_time,end_time)/duration
               )
            )*100
            ,2)
    ,'%') avg_play_progress
from 
tb_user_video_log a join tb_video_info b
on a.video_id=b.video_id
group by b.tag
having avg(
            if(
                timestampdiff(second,start_time,end_time)>=duration,
                1,timestampdiff(second,start_time,end_time)/duration
               )
    )>0.6
order by avg_play_progress desc




Mysql 解法, 执行用时: 36ms, 内存消耗: 6388KB, 提交时间: 2022-01-03

select tag,
concat(round(avg(if(TIMESTAMPDIFF(second,start_time,end_time)>=duration,1,TIMESTAMPDIFF(second,start_time,end_time)/duration))*100,2)
,'%') as avg_play_progress
from tb_user_video_log join tb_video_info t1 using(video_id)
group by tag
having avg(if(TIMESTAMPDIFF(second,start_time,end_time)>=duration,1,TIMESTAMPDIFF(second,start_time,end_time)/duration))>0.6
order by avg_play_progress desc

Mysql 解法, 执行用时: 36ms, 内存消耗: 6396KB, 提交时间: 2021-12-31

select
    t2.tag,
    concat(round(avg(if(timestampdiff(second,t1.start_time,t1.end_time) >= t2.duration,
                        100,(timestampdiff(second,t1.start_time,t1.end_time)/t2.duration) * 100)),2),'%') avg_play_progress
from
    tb_user_video_log t1
join tb_video_info t2
on t1.video_id = t2.video_id
group by t2.tag
having substring_index(avg_play_progress,'%',1) > 60
order by avg_play_progress desc;


select tag,concat(round(avg(case when TIMESTAMPDIFF(second, start_time, end_time)<=duration
                   then TIMESTAMPDIFF(second, start_time, end_time)/duration
               else 1 end)*100,2),'%') as AVG_play_progress
from tb_user_video_log vid
    join tb_video_info info
    on vid.video_id=info.video_id

group by tag
having SUBSTRING_INDEX(AVG_play_progress,"%",1)>60
order by AVG_play_progress desc
              

Mysql 解法, 执行用时: 36ms, 内存消耗: 6412KB, 提交时间: 2022-01-25

select 
b.tag,
concat(round(avg(if(a.dur/b.duration>=1,1,a.dur/b.duration))*100,2),"%")as avg_play_progress
from
(select 
video_id,
TIMESTAMPDIFF(second,start_time,end_time)as dur
from tb_user_video_log)a 
join 
(select video_id,
 tag,
 duration
from tb_video_info)b
on a.video_id = b.video_id
group by b.tag 
having avg(if(a.dur/b.duration>=1,1,a.dur/b.duration))>0.6
order by avg_play_progress desc

Mysql 解法, 执行用时: 36ms, 内存消耗: 6424KB, 提交时间: 2021-12-26

    
SELECT tag,
	concat(round(SUM(play)/COUNT(*)*100,2),'%') AS avg_played_progress
FROM(
	SELECT a.video_id,a.tag,
	  case
	      when TIMESTAMPDIFF(SECOND,start_time,end_time) >= a.duration then 1 
	      else TIMESTAMPDIFF(SECOND,start_time,end_time)/a.duration 
	  end as play
	from tb_video_info a
	left join tb_user_video_log b
	on a.video_id = b.video_id
) AS c
GROUP BY tag
HAVING avg_played_progress> 60
ORDER BY avg_played_progress desc