create table t(x int);
insert into t values(1);
insert into t values(3);
insert into t values(5);
insert into t values(7);
insert into t values(9);
commit;
SQL> select * from t;
         X
----------
         1
         3
         5
         7
         9

举例子说明每种方法,求出“7”这个值。


我先来个第一方种方法:利用子查询的技术
SQL>  select x
  2       from (select x,rownum r 
  3               from(select x from t order by 1 desc)
  4                      where rownum < =2)
  5        where r=2;
         X
----------
         7

方法二、把最大值给T掉,那第2大的值就是老大
      select max(x)
         from t 
           where x<(select max(x) from t);

方法三、 分析函数
       select x
          from (select x,row_number() over(order by x desc) r
                   from t)
       where r=2;


好,我再来说一种方法,这个方法需要点悟性,请看这个语句:
select * 
      from t
       where (select count(*)
               from t t2
                    where t2.x>t.x)=1;
有没有看懂的,好!如果看懂了,请问那求第3大的呢?给你1秒搞定它,哈哈。。。。。。





**********本博客所有内容均为原创,如有转载请注明作者和出处!!!**********
Name:    guoyJoe

QQ:        252803295

Email:    oracledba_cn@hotmail.com

Blog:      http://blog.csdn.net/guoyJoe

ITPUB:   http://www.itpub.net/space-uid-28460966.html

OCM:     http://education.oracle.com/education/otn/YGuo.HTM
 _____________________________________________________________
加群验证问题:哪些SGA结构是必需的,哪些是可选的?否则拒绝申请!!!

答案在:http://blog.csdn.net/guoyjoe/article/details/8624392

Oracle@Paradise  总群:127149411

Oracle@Paradise No.1群:177089463(已满)

Oracle@Paradise No.2群:121341761

Oracle@Paradise No.3群:140856036