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

相关推荐
吴声子夜歌3 分钟前
MongoDB 8.0——索引
数据库·mongodb
影寂ldy15 分钟前
WinForm 完整版分页查询(多条件搜索+页码切换+每页条数切换)
数据库
风途科技~27 分钟前
隧道氮氧化物检测仪|实时监测 NO、NO₂浓度,助力公路隧道通风智能调控
大数据·人工智能
_oP_i40 分钟前
Another Redis Desktop Manager更新
数据库·redis·缓存
ACP广源盛139246256731 小时前
Qwen3.8-Max 开源预期下@ACP#企业级终端硬件演进机遇与 PCIe 交换芯片落地分析
大数据·人工智能·分布式·单片机·嵌入式硬件
宋浮檀s1 小时前
春秋云境——CVE-2022-28512
数据库·安全·web安全
MC皮蛋侠客1 小时前
SQLAlchemy 系列(三):声明式映射与 Schema——让类型、默认值和约束一致
数据库·python
石小千1 小时前
排查MongoDB慢日志问题
数据库·mongodb
安_2 小时前
如何为 RAG 项目选择向量数据库?Milvus、Pinecone、Chroma 怎么选?
数据库·milvus
lupai2 小时前
短信接口快速接入与调用实战指南
java·开发语言·数据库