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

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

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

with tb as (
    select author, date_format(start_time, "%Y-%m") as month,
    sum(case when if_follow = 2 then -1 else if_follow end) / count(*) as fans_growth_rate, 
    sum(case when if_follow = 2 then -1 else if_follow end) as fans_this_month
    from tb_user_video_log uvl
    left outer join tb_video_info vi
    on uvl.video_id = vi.video_id
    where year(start_time) = 2021
    group by author, month
    order by author, month
)
select author, month, round(fans_growth_rate, 3) as fans_growth_rate, sum(fans_this_month) over(partition by author order by month) as total_fans
from tb
order by author, total_fans;

这次被卡在累加一列了,窗口函数可以干这个。

以及我只能在CTE里加年份条件,加在外面 (year(month) = 2021) 跑不了。

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-03 17:37
点赞 评论 收藏
分享
06-12 16:00
天津大学 Java
牛客30236098...:腾讯坏事做尽,终面挂是最破防的 上次被挂了后我连简历都不刷了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
码农索隆:有点耳熟,你们是我教过最差的一届
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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