码迷,mamicode.com
首页 > 数据库 > 详细

SQL,如何查询成绩连续3次上升的同学?

时间:2018-10-16 17:44:16      阅读:355      评论:0      收藏:0      [点我收藏+]

标签:https   net   order by   --   join   inner   ted   art   row   


-- 1.连续3次成绩上升
with t as
(select id,
StudentName,
Score,
row_number() over(partition by StudentName order by id desc) ‘rn‘
from #t)
select distinct a.id,a.StudentName
from t a
inner join t b on a.StudentName=b.StudentName
inner join t c on a.StudentName=c.StudentName
where a.rn=1 and b.rn=2 and c.rn=3
and a.Score>b.Score and b.Score>c.Score

/*
id StudentName
----------- -----------
34 李同学
38 王同学

(2 row(s) affected)
*/


-- 2.连续3次成绩下降
with t as
(select id,
StudentName,
Score,
row_number() over(partition by StudentName order by id desc) ‘rn‘
from #t)
select distinct a.id,a.StudentName
from t a
inner join t b on a.StudentName=b.StudentName
inner join t c on a.StudentName=c.StudentName
where a.rn=1 and b.rn=2 and c.rn=3
and a.Score<b.Score and b.Score<c.Score

/*
id StudentName
----------- -----------
35 赵同学

(1 row(s) affected)
*/

摘自CSDN问答,问答网址:https://bbs.csdn.net/topics/391059033

 

SQL,如何查询成绩连续3次上升的同学?

标签:https   net   order by   --   join   inner   ted   art   row   

原文地址:https://www.cnblogs.com/lumingprince/p/9798740.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!