学生表:

create table hy_student(
   id number(4,0) primary key,
   name nvarchar2(20) not null,
   score number(3,0) not null)

充值:

insert into hy_student
select rownum,dbms_random.string('*',dbms_random.value(1,20)),dbms_random.value(0,100)
from dual
connect by level<201
order by dbms_random.random

第一步把学生按成绩逆序排列:

select * from hy_student order by score desc

第二步给加伪列:

select rownum as rn,a.* from (select * from hy_student order by score desc) a

最后就可以检出前10%的精英了:

select b.* from (select rownum as rn,a.* from (select * from hy_student order by score desc) a) b where b.rn<= (select count(*) from hy_student)/10

结果:

经典SQL问题:Top 10%

--2020-04-02--

以上用到的全部SQL:

create table hy_student(
   id number(4,0) primary key,
   name nvarchar2(20) not null,
   score number(3,0) not null)
   
insert into hy_student
select rownum,dbms_random.string('*',dbms_random.value(1,20)),dbms_random.value(0,100)
from dual
connect by level<201
order by dbms_random.random

commit;

select * from hy_student order by score desc

select rownum as rn,a.* from (select * from hy_student order by score desc) a

select b.* from (select rownum as rn,a.* from (select * from hy_student order by score desc) a) b where b.rn<= (select count(*) from hy_student)/10

 

相关文章:

  • 2022-12-23
  • 2021-07-07
  • 2021-11-06
  • 2021-08-03
  • 2021-09-20
  • 2021-06-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-20
  • 2022-01-14
  • 2022-02-07
  • 2022-02-07
  • 2021-10-11
  • 2022-12-23
  • 2021-12-04
相关资源
相似解决方案