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

试卷完成数同比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,不然无法提交。
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 12:10
点赞 评论 收藏
分享
积极的小学生不要香菜:你才沟通多少,没500不要说难
点赞 评论 收藏
分享
06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
这不纯纯作弊了吗😢😢😢
编程界菜鸡:信这个的这辈子有了,这智商你靠啥都没用
你找工作的时候用AI吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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