文章目录
- 实现一
- 题目来源
谨以此笔记献给浪费掉的两个小时。
此题存在多处疑点和表达错误的地方,如果你看到了这篇文章,劝你跳过该题。
该题对提升HSQL编写能力以及思维逻辑能力毫无帮助。
实现一
with info as (-- 将数据与 video_info 关联,取出 durationselect t1.video_id as video_id,-- 计算观看时长(s)unix_timestamp(end_time) - unix_timestamp(start_time) as view_time,if_like,if_retweet,comment_id,durationfrom video_info t1joinuser_video_log t2on t1.video_id = t2.video_id
)select distinct video_id,-- 计算热度cast(ceil(100 * (seeding_num / video_num) + 5 * like_num + 3 * comment_num +2 * retweet_num) as decimal(16, 1)) as heat
from (-- 计算出每个视频的热度 项select video_id,-- 新鲜度(别问我为什么)1 as freshness,-- 完播数sum(if(view_time = duration, 1, 0)) over (partition by video_id) as seeding_num,-- 该视频播放次数count(1) over (partition by video_id) as video_num,-- 点赞数sum(if_like) over (partition by video_id) as like_num,-- 转发数sum(if_retweet) over (partition by video_id) as retweet_num,-- 评论数sum(if(comment_id is not null, 1, 0)) over (partition by video_id) as comment_numfrom info) t1
order by heat desc
limit 3;
题目来源
http://practice.atguigu.cn/#/question/49/desc?qType=SQL