本文主要是介绍Oracle中取前几名的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
与SQL SERVER相比,Oracle没有Top n 这样简单方便取前几名的方法.但也有相对应的方法可以实现,使用的是Oracle中的伪列:rownum.
最简单的方法如下:
一:最简单的问题:直接取前几名
SELECT A.* from Company_Expense A WHERE ROWNUM<=N
(注意,在上句中我们没有指定按哪列进行排序,而具体rownum序号是依据哪一列,现在我都不确定。但可以明确一点的是,如果我们按主键进行排序,rownum号不会乱,但如果指定其它列的号,序号有可能会乱)
二:麻烦一点的:取后几名
上面方法的一点变通,首先在内部以某列进行排列,在外面取
select V.* from
(
select rownum,A.* from company_expense A
order by a.expenseid desc
)V where rownum<3
三:变态一点的:取前几名,但值相同的记录为同一名次
比如:对班级学生成绩进行排名,第一名:100,共2名,第二名,98,共5名........如此类推,取出排名前十名的所有学生。
基本思路是:先使用Distinct取得成绩的前十名,并使用rownum排序.这样可以得出第十名的值,然后取所有成绩大于等于第十名的所有学生信息。
记录得到后,然后再与刚使用Distinct取得成绩为前十名,且有rownum序号的表Join,以成绩为条件。就可以得到正常的排名次序。
相关代码请参考如下脚本:
select rownum, T.Row_Num, v.*
from (select A.Name,
A.Employee_Code,
v.STATUS,
To_Char(v.JOIN_DATE, 'yyyy-mm-dd') Join_Date,
V.Unit_Name,
p.line_description Line_Num,
sum(a.last_result) Sum_result
from QM_QUALITY_LEVEL_JUDGE A,
hr_lbr_employee_tl_tbl_v v,
Qm_Product_Line P
Where a.employee_code = v.EMPLOYEE_CODE
And A.LINE_NUM = P.Line_Num
And To_Char(A.Index_Date, 'yyyy-mm-dd') >= '2006-05-01'
And To_Char(A.Index_Date, 'yyyy-mm-dd') <= '2006-07-24'
And A.Last_Result > 0
group by A.Name,
A.Employee_Code,
V.STATUS,
V.JOIN_DATE,
V.Unit_Name,
A.Line_Num,
P.LINE_DESCRIPTION
order by Sum_Result desc, employee_code desc) v ---基本的信息
left join (select rownum row_Num, v.*
from (select distinct sum(a.last_result) Sum_result
from QM_QUALITY_LEVEL_JUDGE A,
hr_lbr_employee_tl_tbl_v v,
Qm_Product_Line P
Where a.employee_code = v.EMPLOYEE_CODE
And A.LINE_NUM = P.Line_Num
And To_Char(A.Index_Date, 'yyyy-mm-dd') >=
'2006-05-01'
And To_Char(A.Index_Date, 'yyyy-mm-dd') <=
'2006-07-24'
And A.Last_Result > 0
group by A.Name,
A.Employee_Code,
V.STATUS,
V.JOIN_DATE,
V.Unit_Name,
A.Line_Num,
P.LINE_DESCRIPTION
Order by Sum_Result Desc) v
Where rownum <= 10) T On v.Sum_Result = T.Sum_Result --得到正确的排名序号
-----以下条件是取得所有大于第十名成绩的记录
where V.sum_Result >=
(select min(Sum_Result) Sum_Result
from (select rownum row_Num, v.*
from (select distinct sum(a.last_result) Sum_result
from QM_QUALITY_LEVEL_JUDGE A,
hr_lbr_employee_tl_tbl_v v,
Qm_Product_Line P
Where a.employee_code = v.EMPLOYEE_CODE
And A.LINE_NUM = P.Line_Num
And To_Char(A.Index_Date, 'yyyy-mm-dd') >=
'2006-05-01'
And To_Char(A.Index_Date, 'yyyy-mm-dd') <=
'2006-07-24'
And A.Last_Result > 0
group by A.Name,
A.Employee_Code,
V.STATUS,
V.JOIN_DATE,
V.Unit_Name,
A.Line_Num,
P.LINE_DESCRIPTION
Order By Sum_Result Desc) v
Where rownum <= 10))
from (select A.Name,
A.Employee_Code,
v.STATUS,
To_Char(v.JOIN_DATE, 'yyyy-mm-dd') Join_Date,
V.Unit_Name,
p.line_description Line_Num,
sum(a.last_result) Sum_result
from QM_QUALITY_LEVEL_JUDGE A,
hr_lbr_employee_tl_tbl_v v,
Qm_Product_Line P
Where a.employee_code = v.EMPLOYEE_CODE
And A.LINE_NUM = P.Line_Num
And To_Char(A.Index_Date, 'yyyy-mm-dd') >= '2006-05-01'
And To_Char(A.Index_Date, 'yyyy-mm-dd') <= '2006-07-24'
And A.Last_Result > 0
group by A.Name,
A.Employee_Code,
V.STATUS,
V.JOIN_DATE,
V.Unit_Name,
A.Line_Num,
P.LINE_DESCRIPTION
order by Sum_Result desc, employee_code desc) v ---基本的信息
left join (select rownum row_Num, v.*
from (select distinct sum(a.last_result) Sum_result
from QM_QUALITY_LEVEL_JUDGE A,
hr_lbr_employee_tl_tbl_v v,
Qm_Product_Line P
Where a.employee_code = v.EMPLOYEE_CODE
And A.LINE_NUM = P.Line_Num
And To_Char(A.Index_Date, 'yyyy-mm-dd') >=
'2006-05-01'
And To_Char(A.Index_Date, 'yyyy-mm-dd') <=
'2006-07-24'
And A.Last_Result > 0
group by A.Name,
A.Employee_Code,
V.STATUS,
V.JOIN_DATE,
V.Unit_Name,
A.Line_Num,
P.LINE_DESCRIPTION
Order by Sum_Result Desc) v
Where rownum <= 10) T On v.Sum_Result = T.Sum_Result --得到正确的排名序号
-----以下条件是取得所有大于第十名成绩的记录
where V.sum_Result >=
(select min(Sum_Result) Sum_Result
from (select rownum row_Num, v.*
from (select distinct sum(a.last_result) Sum_result
from QM_QUALITY_LEVEL_JUDGE A,
hr_lbr_employee_tl_tbl_v v,
Qm_Product_Line P
Where a.employee_code = v.EMPLOYEE_CODE
And A.LINE_NUM = P.Line_Num
And To_Char(A.Index_Date, 'yyyy-mm-dd') >=
'2006-05-01'
And To_Char(A.Index_Date, 'yyyy-mm-dd') <=
'2006-07-24'
And A.Last_Result > 0
group by A.Name,
A.Employee_Code,
V.STATUS,
V.JOIN_DATE,
V.Unit_Name,
A.Line_Num,
P.LINE_DESCRIPTION
Order By Sum_Result Desc) v
Where rownum <= 10))
这篇关于Oracle中取前几名的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!