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

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

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

解题思路:

1 先把关键列列出来:作者,年月份,涨粉,掉粉这些比较好统计的

关于涨粉,掉粉计算:配合group by + sum(if()) 或者cast when 直接计算

2 难点在于粉丝累计数量,这里是需要开分,sum() over(partition by 作者 order by 年月)进行第二步计算

答案如下:

select
c.author,
c.month,
cast( ((c.gz + c.qg) / c.guank)  as decimal(10,3) ) as rate,
sum(c.gz) over( partition by c.author order by c.month ) + sum(c.qg) over ( partition by c.author order by c.month ) as total
from
(select
b.author,
date_format(a.start_time,'%Y-%m') as month,
sum(if((a.if_follow = '1'),1,0)) as gz,
sum(if((a.if_follow = '2'),-1,0)) as qg,
count(1) as guank
from tb_user_video_log a 
left join tb_video_info b
on a.video_id=b.video_id
where year(a.start_time) = '2021'
group by b.author,date_format(a.start_time,'%Y-%m'))c
order by c.author, total 

全部评论

相关推荐

在笔试的大西瓜很矫健:这跟数分八竿子打不着,先去了解实习要会什么再说找实习吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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