题解 | #连续两次作答试卷的最大时间窗#

试卷完成数同比2020年的增长率及排名变化

http://www.nowcoder.com/practice/13415dff75784a57bedb6d195262be7b

select aa.tag,
      t1.exam_cnt_20,
      t2.exam_cnt_21,
      concat(round((t2.exam_cnt_21 - t1.exam_cnt_20) / t1.exam_cnt_20 * 100, 1), '%') as growth_rate,
      rank1,
      rank2,
      # 注意一般数据库默认都是unsigned, 是不能出现负数的, 可用cast(字段 as signed)即可
      cast(rank2 as SIGNED) - cast(rank1 as SIGNED) as exam_cnt_rank
  from (
      select exam_id,
          count(submit_time) exam_cnt_20,
          # 备注: 这里是不能用partition的, 因为是根据分组之后的exam_id进行的
          rank() over(order by count(submit_time) desc) as rank1
      from exam_record
      where year(start_time)=2020
      and month(start_time) <= 6
      group by exam_id
      having exam_cnt_20 >= 1
  ) t1
  join (
      select exam_id,
          count(submit_time) exam_cnt_21,
          rank() over(order by count(submit_time) desc) as rank2
      from exam_record
      where year(start_time)=2021
      and month(start_time) <= 6
      group by exam_id
      having exam_cnt_21 >= 1
  ) t2 on t1.exam_id=t2.exam_id
  join examination_info aa on t1.exam_id=aa.exam_id
  order by growth_rate desc, t2.exam_cnt_21
 cast(rank2 as SIGNED) - cast(rank1 as SIGNED) as exam_cnt_rank,这里不能rank2-rank1,不然无法提交。
全部评论

相关推荐

10-17 10:05
已编辑
北华大学 全栈开发
牛客872465272号:掉头发了哥
点赞 评论 收藏
分享
牛舌:如果我不想去,不管对方给了多少,我一般都会说你们给得太低了。这样他们就会给下一个offer的人更高的薪资了。
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务