在Oracle 8i中,可以这样写:
Select 字段 From (Select 字段 From 表 Where Nvl(字段,0)<>0 Order by 字段 Desc) Where Rownum<=10
在8.0.5及以下版本中不能这伴写(子查询中的Order by)。
不过只用Select应该有办法,我想想先。
人气真旺!
我使用的为Oracle 8i。
用Richter的方法改进如下即可:
Select aa.Emp_no,aa.Amount
from t_S_Salary aa,
(Select a.Emp_no
From t_S_Salary a,t_S_Salary b
Where a.Amount<=b.Amount
Group by a.Emp_no
Having Count(b.Emp_no)<= 11 ) bb
where aa.Emp_no = bb.Emp_no
and rownum <= 10
order by aa.Amount desc
但在amount为22的员工有3位时,你只能将11继续加大。
当然还要考虑amount大于22员工数!
最简省的方法,将11改为1000或更大。
此种方法效率...
我推荐一种方法。
1、创建视图
create view v_s_salary as
select emp_no,sum(amount) total
from t_s_salary
group by emp_no
order by sum(amount) desc;
2、
select * from v_s_salary
where rownum <= 10;
假设如下:
表名: t_Salary
字段: Emp_no 人员编号,
Amount 收入
数据库 Oracle8 or later
Select * from t_Salary aa,
(Select a.Emp_no
From t_Salary a,t_Salary b
Where a.Amount<=b.Amount
Group by a.Emp_no
Having Count(b.Emp_no)<=10) bb
where aa.Emp_no = bb.Emp_no
order by aa.Amount desc
我试过:以下是测试Sql,在Oracle8i下试。
Create table t_S_Salary(
Emp_no varchar(10),
Amount number(8)
)
/
Insert into t_S_Salary
Values('00001',1)
/
Insert into t_S_Salary
Values('00002',2)
/
Insert into t_S_Salary
Values('00003',3)
/
Insert into t_S_Salary
Values('00004',4)
/
Insert into t_S_Salary
Values('00005',5)
/
Insert into t_S_Salary
Values('00006',6)
/
Insert into t_S_Salary
Values('00007',7)
/
Insert into t_S_Salary
Values('00008',8)
/
Insert into t_S_Salary
Values('00009',9)
/
Insert into t_S_Salary
Values('00010',10)
/
Insert into t_S_Salary
Values('00011',11)
/
Insert into t_S_Salary
Values('00012',12)
/
Insert into t_S_Salary
Values('00013',13)
/
Insert into t_S_Salary
Values('00014',14)
/
Insert into t_S_Salary
Values('00015',15)
/
Select aa.Emp_no,aa.Amount
from t_S_Salary aa,
(Select a.Emp_no
From t_S_Salary a,t_S_Salary b
Where a.Amount<=b.Amount
Group by a.Emp_no
Having Count(b.Emp_no)<=10) bb
where aa.Emp_no = bb.Emp_no
order by aa.Amount desc
/
Drop table t_S_Salary
/
赚点分太辛苦了,你赶紧给点分吧。