查询每个部门收入排名第二高的员工姓名

解决方案 »

  1.   


    create table tb1 (bm varchar2(20),yg varchar2(20),PRICE NUMBER);insert into tb1 values ('工程部','张三',5550);
    insert into tb1 values ('开发部','李四',8230);
    insert into tb1 values ('开发部','王五',7700);
    insert into tb1 values ('开发部','侯柳',6500);
    insert into tb1 values ('工程部','马奇',4400);
    insert into tb1 values ('财务部','幻觉',3500);
    insert into tb1 values ('财务部','阿斯',4300);
    insert into tb1 values ('财务部','大大',3200);
    insert into tb1 values ('工程部','宝宝',5100);
    insert into tb1 values ('工程部','人人',4800);
    insert into tb1 values ('工程部','咪咪',6650);select  bm,yg,price from 
    (select bm,yg,price,row_number() over(partition by bm order by price) rn
    from tb1)
    where rn=2
           bm     yg     price 
    ----------------------------------------
    1 财务部 幻觉 3500
    2 工程部 人人 4800
    3 开发部 王五 7700
      

  2.   

    失误  掉了个排序desc
    select bm,yg,price from 
    (select bm,yg,price,row_number() over(partition by bm order by price desc) rn
    from tb1)
    where rn=2
      

  3.   

    select *
      From tb1 t1
     where (select count(distinct(t2.price))
              from tb1 t2
             where t2.bm = t1.bm
               and t2.price > t1.price) = 1;