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

Posted 路鸣

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL,如何查询成绩连续3次上升的同学?相关的知识,希望对你有一定的参考价值。


-- 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次上升的同学?的主要内容,如果未能解决你的问题,请参考以下文章

SQL语句:查询各班成绩前3的同学姓名

SQL语句练习查询平均成绩最高的前3名同学

sql计算机系的平均成绩为多少?

sql语句课程练习

50条SQL查询技巧查询语句示例

sql语句课程练习