Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: SQL - Select top 20 records
In article <36EE5FE5.3E77_at_one.net>,
Hari Vattyam <hvattyam_at_one.net> writes:
> John Haskins wrote:
Assuming your table is called tableA, try something like this:
select membno,sum(paid_amt) paidamt
from tableA t1
where 20 > ( select count(*)
from tableA t2 group by membno having sum(paid_amt) > ( select sum(paid_amt) from tableA t3 where t3.membno = t1.membno ) )
Note that this query might perform badly, though, due to the double nested correlated subqueries.
Do you know the standard 'emp' table used in many Orcale courses? I once got the question 'Which people earn the top 3 salaries?'. This was the query to get the answer:
select ename, sal
from emp e1
where 3 > ( select count(distinct sal)
from emp e2 where e2.sal > e1.sal )
This would give you this:
ENAME SAL
---------- ----------
KING 5000 SCOTT 3000 FORD 3000 JONES 2975
Substituting the 'count(distinct sal)' with 'count(*)' would give you the three people earning the highest salaries instead of the people earning the three highest salaries.
> Sounds to me like the classic ranking problem which cannot be > done in SQL only. The Fox Pro query you show actually does the > manipulation (post-processing) after the result set from the group > by is available. Your only choice seems to be to artie a procedure > which will rank the records in the order you want.
Ya think so? :-)
Remco
--
rd31-144: 8:00pm up 3 days, 2:23, 5 users, load average: 1.08, 1.14, 1.16
Received on Tue Mar 16 1999 - 13:20:52 CST
![]() |
![]() |