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

明确题意:

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


问题分解:

  • 关联用户-视频互动记录和短视频信息表:JOIN tb_video_info USING(video_id);
  • 按视频类别分组:GROUP BY tag
  • 计算每个类别的平均播放进度:
    • 播放进度=播放时长÷视频时长*100%
    • 播放时长=TIMESTAMPDIFF(SECOND, start_time, end_time);特殊情况:播放时长大于视频时长时,播放进度为100%(加个IF判断)
    • 平均进度=AVG(每个进度)
    • 结果保留2位小数:ROUND(x, 2)
    • 百分比格式化:CONCAT(x, '%')
    • 计算公式:
    ROUND(AVG(
        IF(TIMESTAMPDIFF(SECOND, start_time, end_time) > duration, 1,
           TIMESTAMPDIFF(SECOND, start_time, end_time) / duration)
    ) * 100, 2) as avg_play_progress
  • 筛选播放进度>60%的视频类别:HAVING avg_play_progress > 60

细节问题:

  • 表头重命名:as
  • 按播放进度倒序排序:ORDER BY avg_play_progress DESC;

完整代码:

SELECT tag, CONCAT(avg_play_progress, "%") as avg_play_progress
FROM (
    SELECT tag, 
        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 USING(video_id)
    GROUP BY tag
    HAVING avg_play_progress > 60
    ORDER BY avg_play_progress DESC
) as t_progress;