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

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

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

全部评论

相关推荐

06-27 12:30
延安大学 C++
实习+外包,这两个公司底层融为一体了,如何评价呢?
一表renzha:之前面了一家外包的大模型,基本上都能答出来,那面试官感觉还没我懂,然后把我挂了,我都还没嫌弃他是外包,他把我挂了……
第一份工作能做外包吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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