tab1 表中数据如下 id    val 1      10 2      11
 3      14
请问如何查询val第二大的那条数据
是select id from tab1 where rownum=1 order by val desc 这样对么

解决方案 »

  1.   

    select id from tab1 where rownum=2 order by val desc 
      

  2.   

    select id from ( select rownum no,id from tab1 where rownum <=2 order by val desc ) t where no=1 order by val 
      

  3.   


    这个少了个东西,下面这个补充上了
    select id from ( select rownum no,id ,val  from tab1 where rownum <=2 order by val desc ) t where no=1 order by val 
      

  4.   

    select id,val from (select id,val,ROW_NUMBER() OVER (ORDER BY val) as rn from tab1) where rn=2
      

  5.   

    create table tab1(id int,val int);
    insert into tab1 values(1,10);
    insert into tab1 values(2,11);
    insert into tab1 values(3,14);select * from tab1 a
    where id not in (select max(id) from tab1)
    and  exists(select 1 from tab1 where a.id>id)