经典SQL问题:Top 10%

学生表:

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

结果:

--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
复制代码

 

posted @   逆火狂飙  阅读(1270)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2018-04-02 【pyhon】nvshens按目录图片批量下载爬虫1.00(多线程版)
2018-04-02 【Canvas与密铺】90年代马赛克密铺效果 1920x1080
2015-04-02 【Canvas与化学】铁元素图标
2015-04-02 上传项目至svn服务器,从svn上获取项目
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示