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

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

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

  1. select 
  2. author,
  3. date_format(start_time,'%Y-%m') month,
  4. round(sum(case when if_follow = 1 then 1
  5.    when if_follow = 2 then -1
  6.    else 0 end) /count(start_time), 3) fans_growth_rate,
  7. sum(sum(case when if_follow = 1 then 1
  8.    when if_follow = 2 then -1
  9.    else 0 end)) over
  10.    (partition by author order by date_format(start_time,'%Y-%m')) total_fans
  11.     from 
  12.     tb_user_video_log join tb_video_info using(video_id)
  13.     where year(start_time) = 2021
  14.     group by author, month
  15.     order by author, total_fans
  16. 注意的几个点:
  • 窗口函数中order by的内容要和date_format(start_time,'%Y-%m') month一样,
  • 为了求得总的粉丝量,所以需要用窗口函数将粉丝数量作者和月份进行划分
  • 可以在sum()里面加case when直接进行计算,这样就不需要用子查询了
全部评论

相关推荐

AAA专业长城贴瓷砖刘大爷:这样的简历我会直接丢进垃圾桶,花里胡哨的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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