49. 视频热度问题

文章目录

谨以此笔记献给浪费掉的两个小时。

此题存在多处疑点和表达错误的地方,如果你看到了这篇文章,劝你跳过该题。

该题对提升HSQL编写能力以及思维逻辑能力毫无帮助。

实现一

sql 复制代码
with info as (
    -- 将数据与 video_info 关联,取出 duration
    select t1.video_id                                           as video_id,
           -- 计算观看时长(s)
           unix_timestamp(end_time) - unix_timestamp(start_time) as view_time,
           if_like,
           if_retweet,
           comment_id,
           duration
    from video_info t1
             join
         user_video_log t2
         on 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_num
         from info
     ) t1
order by heat desc
limit 3;

题目来源

http://practice.atguigu.cn/#/question/49/desc?qType=SQL

相关推荐
多弗朗皮卡丘7 小时前
MySQL表的约束
数据库·mysql
Flynn Liang11 小时前
AI Agent Redis 生产踩坑实录:13 类故障的根因、复现与调优
数据库·人工智能·redis·缓存·ai agent·故障分析
SelectDB12 小时前
开源日志平台怎么选?ELK、Loki、ClickHouse、Apache Doris 四大方案对比与选型指南
数据库
SelectDB12 小时前
ELK 磁盘空间不足怎么办?Elasticsearch 存储膨胀原因、排查步骤与 3 种降本方案
数据库
满小满13 小时前
为什么我先 ANALYZE,而不是先加索引
数据库·dba
SelectDB13 小时前
日志分析用什么数据库好?Elasticsearch、ClickHouse、Doris 选型对比与 3 个企业案例
数据库
inferno15 小时前
十三、存储过程(PROCEDURE)
数据库·sql·mysql
数据库急诊室_阿哲15 小时前
一周慢查询日志审计:1200 条里有多少真问题
运维·数据库·dba
zhangjw3416 小时前
2-4 集合运算 union /union all /intersect/except
数据库·postgresql
Decay__16 小时前
PostgreSQL DBA 日常运维手册 —— 会话、锁阻塞、慢查询、膨胀与每日巡检
运维·数据库·sql·postgresql