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

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

https://www.nowcoder.com/practice/d337c95650f640cca29c85201aecff84?tpId=268&tqId=2285069&ru=/exam/oj&qru=/ta/sql-factory-interview/question-ranking&sourceUrl=%2Fexam%2Foj

select author,date_format(start_time,'%Y-%m') as month,
       round(sum(if(if_follow=2,-1,if_follow))/count(if_follow),3) as fans_growth_rate,
       sum(sum(case when if_follow=1 then 1
             when if_follow=2 then -1
             else 0 end)) over(partition by author order by date_format(start_time,'%Y-%m')) 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 author,month
order by author,total_fans

#终于理解两层sum的含义啦:内层sum是跟group by 搭配的,外层sum是跟我们窗口函数搭配的。先运行内层再运行外层!

全部评论

相关推荐

09-29 00:03
门头沟学院 Java
点赞 评论 收藏
分享
今天要投简历吗? 国庆期间真的不投比较好吗? emmmmmm
想要offer的每一...:可以去投,谁回你,你就拉黑他,国庆还上班,贼黑心
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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