日期:2014-05-18  浏览次数:20693 次

oracle高手看过来。
问一下oracle用没有这样的函数,
比如说要查自己的成绩的在全班成绩的名次

或者别的解决方法?

------解决方案--------------------
所有的sql都没有这样的函数
  给你写一吧
select *,(select count(*)+1 from 表 as x where x.成绩> t.成绩) as '名次 '
from 表 as t where 学生id =6 order by 成绩 desc
------解决方案--------------------
create table temptable
(NAME varchar2(10),
SCORE float
);

SQL> insert into temptable values( 'aaa ',88.8);

已创建 1 行。

SQL> insert into temptable values( 'bbb ',88.8);

已创建 1 行。

SQL> insert into temptable values( 'ccc ',99.9);

已创建 1 行。

SQL> insert into temptable values( 'ddd ',34.9);

已创建 1 行。

SQL> insert into temptable values( 'eee ',65.5);

已创建 1 行。

SQL> commit;

提交完成。

select name,score,row_number() over(order by score asc) as "成绩排名 " from temptable;

NAME SCORE ROWNUMBER
---------- ---------- ----------
ddd 34.9 1
eee 65.5 2
aaa 88.8 3
bbb 88.8 4
ccc 99.9 5

--假如lz的name为:aaa,全班排名应为:3
select * from (select name,score,row_number() over(order by score asc) as "我的成绩排名 " from temptable) where name= 'aaa ';

NAME SCORE ROWNUMBER
---------- ---------- ----------
aaa 88.8 3

------解决方案--------------------
查询结果:

select name,score,row_number() over(order by score asc) as "成绩排名 " from temptable;

NAME SCORE 成绩排名
---------- ---------- ----------
ddd 34.9 1
eee 65.5 2
aaa 88.8 3
bbb 88.8 4
ccc 99.9 5

--假如lz的name为:aaa,全班排名应为:3
select * from (select name,score,row_number() over(order by score asc) as "我的成绩排名 " from temptable) where name= 'aaa ';

NAME SCORE 我的成绩排名
---------- ---------- ------------
aaa 88.8 3

------解决方案--------------------
换成了dense_rank()分析函数,这样成绩相同者排名也相同。

select name,score,dense_rank() over(order by score desc) as "成绩排名 " from temptable;

NAME SCORE 成绩排名
---------- ---------- ----------
ccc 99.9 1
aaa 88.8 2
bbb 88.8 2
eee 65.5 3
ddd 34.9 4

--假如lz的name为:aaa,全班排名应为:2
select * from (select name,score,dense_rank() over(order by score desc) as "我的成绩排名 " from temptable) where name= 'aaa ';

NAME SCORE 我的成绩排名
---------- ---------- ------------
aaa 88.8 2