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

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

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

首先计算出每个作者每个月的新增的粉丝数量,count(tu.if_follow)计算出tb_user_video_log表中所有的播放量,综合算出涨粉率

select 
    tv.author
    ,DATE_FORMAT(tu.end_time,"%Y-%m") month
    ,round((sum(case when tu.if_follow = 1 then 1 when tu.if_follow = 2 then -1 else 0 end) / count(tu.if_follow)),3) fans_growth_rate
from tb_user_video_log tu
join tb_video_info tv
on tu.video_id = tv.video_id
group by tv.author, month

比较难的是计算出当前的粉丝量,结合sum() over()来进行计算,将当前的新粉丝和之前的粉丝结合起来,变成总的粉丝数量

sum(sum(case when tu.if_follow = 1 then 1 when tu.if_follow = 2 then -1 else 0 end)) over(partition by tv.author order by DATE_FORMAT(tu.end_time,"%Y-%m"))

将sql结合起来,先过滤出2021年的数据,并根据author和total_fans进行排序,不这样排序过不了。。。

select 
    tv.author
    ,DATE_FORMAT(tu.end_time,"%Y-%m") month
    ,round((sum(case when tu.if_follow = 1 then 1 when tu.if_follow = 2 then -1 else 0 end) / count(tu.if_follow)),3) fans_growth_rate
    ,sum(sum(case when tu.if_follow = 1 then 1 when tu.if_follow = 2 then -1 else 0 end)) over(partition by tv.author order by DATE_FORMAT(tu.end_time,"%Y-%m")) total_fans
from tb_user_video_log tu
join tb_video_info tv
on tu.video_id = tv.video_id
where DATE_FORMAT(tu.end_time,"%Y") = '2021'
group by tv.author, month
order by tv.author, total_fans
全部评论

相关推荐

2024-11-11 15:12
南昌大学 材料工程师
一个菜鸡罢了:哥们,感觉你的简历还是有点问题的,我提几点建议,看看能不能提供一点帮助 1. ”新余学院“别加粗,课程不清楚是否有必要写,感觉版面不如拿来写一下做过的事情,教育经历是你的弱势就尽量少写 2. “干部及社团经历”和“自我评价”删掉 3. 论文后面的“录用”和“小修”啥的都删掉,默认全录用,问了再说,反正小修毕业前肯定能发出来 4. 工作经验和研究成果没有体现你的个人贡献,着重包装一下个人贡献
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务