日期:2014-05-17  浏览次数:20835 次

SQL求解
SQL> select * from t1;
 
NAME SCORE
---------- -----
AA 67
BB 85
CC 98
DD 58
EE 72
FF 85
GG 47

想得到的结果:

NAME SCORE RANK
---------- ----- ----------
CC 98 1
BB 85 1
FF 85 1
EE 72 1
AA 67 2
DD 58 3
GG 47 4

------解决方案--------------------
SQL code

--测试表创建以及测试数据
create table t_tabletwo
(
 name varchar2(10),
 score number
)

insert into t_tabletwo
select 'AA',67 from dual union all
select 'BB',85 from dual union all
select 'CC',98 from dual union all
select 'DD',58 from dual union all
select 'EE',72 from dual union all
select 'FF',85 from dual union all
select 'GG',47 from dual 
--查询语句
select name,
       score,
       1 as rank
from t_tabletwo 
where score>=70
union all
select name,
       score,
       rownum+1 as rank
from t_tabletwo 
where score <70
order by score desc
--查询结果
CC    98    1
BB    85    1
FF    85    1
EE    72    1
AA    67    2
DD    58    3
GG    47    4

------解决方案--------------------
SQL code

select t.name,t.score,
    dense_rank() 
    over (order by case when score>69 then 100 else score end  desc) rank
from t_tabletwo t