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

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

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

select 
    b.author as author,
    date_format(a.start_time,'%Y-%m') as month,
    round(sum(case 
                when a.if_follow = 1 then 1
                when a.if_follow = 2 then -1 else 0 end)/
    count(a.start_time),3) as 	fans_growth_rate,
    sum(sum(case 
                when a.if_follow = 1 then 1
                when a.if_follow = 2 then -1 else 0 end))over(partition by author order by date_format(a.start_time,'%Y-%m')) as total_fans

from
    tb_user_video_log a left join tb_video_info b 
    on a.video_id = b.video_id
where 
    year(start_time) = 2021
group by 
    b.author,
    date_format(a.start_time,'%Y-%m')
order by author,total_fans

sum([distinct] <expr>) over ([partition_clause] [orderby_clause] [frame_clause])

sum开窗函数注意点:

1、开窗但是不排序:sum([distinct] <expr>) over ([partition_clause])

返回当前窗口累计汇总值,同一个窗口内的sum开窗值是相同的

2、开窗也排序:sum([distinct] <expr>) over ([partition_clause] [orderby_clause] [frame_clause])

返回当前窗口从开始行到当前行的累计汇总值

全部评论

相关推荐

12-02 20:08
已编辑
门头沟学院 后端工程师
notbeentak...:孩子,说实话,选择很重要,可能你换一个方向会好很多,但是现在时间不太够了,除非准备春招
点赞 评论 收藏
分享
10-21 00:37
已编辑
门头沟学院 C++
小浪_Coding:你问别人,本来就是有求于人,别人肯定没有义务免费回答你丫, 有点流量每天私信可能都十几,几十条的,大家都有工作和自己的事情, 付费也是正常的, 就像你请别人搭把手, 总得给人家买瓶水喝吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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