Oracle之Top写法
由于Oracle不支持select top 语句,所以在Oracle中经常是用order by 跟rownum的组合来实现select top n的查询。
实现方法如下所示:
select 列名1 ...列名n from
(
select 列名1 ...列名n
from 表名 order by 列名1
)
where rownum <=N(抽出记录数)
order by rownum asc
举例,学生分数表数据如下图所示:
1、按分数由高到低排序取出前5名
select s_id,s_c
from (select s_id,s_c from temp_test order by s_c desc) t
where rownum <= 5
order by rownum
执行后如图所示即为所求:
2、如下图是学生分数数据且分数从高到低,要求取分数排名4-8名数据
利用分析函数row_number() over ( partition by col1 order by col2 )
select s_id,s_c
from (select s_id,s_c,row_number() over (order by s_c desc) rn from temp_test) t
where rn between 4 and 8;
本站内容来源于作者发布和网络转载,如有版权相关问题请及时与我们取得联系,我们将立即删除。