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

相关推荐
Databend43 分钟前
Databend 亮相 RustChinaConf 2025,分享基于 Rust 构建商业化数仓平台的探索
数据库
得物技术2 小时前
破解gh-ost变更导致MySQL表膨胀之谜|得物技术
数据库·后端·mysql
Lx3522 小时前
复杂MapReduce作业设计:多阶段处理的最佳实践
大数据·hadoop
武子康5 小时前
大数据-100 Spark DStream 转换操作全面总结:map、reduceByKey 到 transform 的实战案例
大数据·后端·spark
expect7g6 小时前
Flink KeySelector
大数据·后端·flink
Raymond运维6 小时前
MariaDB源码编译安装(二)
运维·数据库·mariadb
沢田纲吉6 小时前
🗄️ MySQL 表操作全面指南
数据库·后端·mysql
RestCloud1 天前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
阿里云大数据AI技术1 天前
StarRocks 助力数禾科技构建实时数仓:从数据孤岛到智能决策
大数据
RestCloud1 天前
为什么说零代码 ETL 是未来趋势?
数据库·api