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

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

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

select b.author,date_format(a.start_time,'%Y-%m') as month,
round(sum(case when a.if_follow=1 then 1 when a.if_follow=0 then 0 when a.if_follow=2 then -1 end)/count(a.video_id),3) as fans_growth_rate,
sum(sum(case when a.if_follow=1 then 1 when a.if_follow=0 then 0 when a.if_follow=2 then -1 end))over(partition by b.author order by date_format(a.start_time,'%Y-%m')) as total_fans
from tb_user_video_log as a
left join tb_video_info as b
on a.video_id=b.video_id
where year(start_time)=2021
group by b.author,month
order by b.author,total_fans

这个题学到了三个点:

1⃣️date_format函数如果是%y则是两位数,如果是%Y就是四位数

2⃣️case when可以多次用,直接并列when即可

3⃣️有个疑问:为什么这里不用窗口函数计算粉丝数不行,想了一下应该是求和之后再对author求和

全部评论

相关推荐

码农索隆:我头回见校招简历把个人优势写在最前面的,是我老了吗
点赞 评论 收藏
分享
头像
03-20 22:00
重庆大学 Java
适彼乐土:“他们不行再找你” 最后的底牌吗?有点意思
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务