题解 | #每个创作者每月的涨粉率及截止当前的总粉丝量#

每个创作者每月的涨粉率及截止当前的总粉丝量

https://www.nowcoder.com/practice/d337c95650f640cca29c85201aecff84

这个题比较简单,只注意一下只统计2021年的数据,我提交没注意到2021错了一次。

层次从内向外

第一层:join 两张表,过滤掉不需要的数据,然后分组,分组的时候可以直接计算出fans_growth_rate,同时计算出monthTotalFans,这个是这个月增加的粉丝数量,在第二层开窗统计需要用到。

第二层,开个窗,统计截止到当前月份用户的粉丝数量。

题目中描述还是清晰,明确,但是并没有说明一点,是否存在跳跃月份的问题

#2021  每个创作者,每个月

select
    c.author,
    c.month,
    c.fans_growth_rate, 
    sum(c.monthTotalFans) over(partition by c.author order by c.month) as total_fans
from 
(
    select

    a.author,
    date_format(b.start_time,"%Y-%m") as month,
    format(sum(case when b.if_follow = 1 then 1 when b.if_follow = 2 then -1 else 0 end)/count(1),3) as fans_growth_rate,
    sum(case when b.if_follow = 1 then 1 when b.if_follow = 2 then -1 else 0 end) as monthTotalFans
    from tb_video_info a join tb_user_video_log b on a.video_id = b.video_id and year(b.start_time)=2021

    group by a.author,date_format(b.start_time,"%Y-%m")
) c
order by c.author, total_fans

全部评论

相关推荐

2025-12-14 11:43
黑龙江大学 Java
用微笑面对困难:确实比较烂,可以这么修改:加上大学的qs排名,然后大学简介要写一些,然后硕士大学加大加粗,科研经历第一句话都写上在复旦大学时,主要负责xxxx,简历左上角把学校logo写上,建议用复旦大学的简历模板
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务