Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: top N or rewriting MAX

Re: top N or rewriting MAX

From: Roger Wernersson <roger.wernersson_at_adra.se>
Date: 1997/01/07
Message-ID: <32D226A1.13A4@adra.se>#1/1

otierney_at_freenet.edmonton.ab.ca wrote:
> Does anyone know of good (ie FAST) ways of getting the top
> N rows in a table?

This is actually quite simple. This example assumes you have a table MY_TABLE with a numeric column VALUE. The SELECT statement will give you the two lowest values.

SELECT t1.value
FROM my_table t1, my_table t2
WHERE t1.value <= t2.value
GROUP BY t1.value
HAVING COUNT (*) > (SELECT COUNT (*) - 2 FROM my_table);

I don't know how it performs on large tables but it seems to work. I haven't used it in any of my own applications but the problem interests me. :-)

/Roger

-- 
Sport radio: people listening to people watching people having fun
Mailto:roger.wernersson_at_adra.se
BTW: All opinions are mine, all mine, and nobody's but mine.
Received on Tue Jan 07 1997 - 00:00:00 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US