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: Komix Hui <huikomix_at_dt.com.hk>
Date: 1997/01/15
Message-ID: <32DD86C7.56BF@dt.com.hk>#1/1

Roger Wernersson wrote:
>
> Roger Wernersson wrote:
> > 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.
>
> You stupid! You are dead wrong! It is much simpler to create a view with
> a GROUP BY clause and then use ROWNUM to get the lowest values, though
> you might be in for some trouble if you want the highest ones.
>
> CREATE VIEW test_view AS
> SELECT value
> FROM test
> GROUP BY value;
>
> SELECT value
> FROM test
> WHERE ROWNUM < 10;

Before you say other people stupid, you should be sure your answer is correct.

I do not think your answer will work.

Try this:

    SELECT Column_1

       FROM Table_1 A
       WHERE  10 < (SELECT COUNT(*)
			FROM Table_1 B
			WHERE A.Column > B.Column);

to select top 10 rows.

If 'column_1' is not unique, you should adjust the statement with '>=' and '<='.

-- 
Hui, Kwok Miu (Xu, Guo Miao)         E-Mail: huikomix_at_dt.com.hk
Assistant MIS Manager                Tel   : (00852)2589 3369
Cho Yang (Hong Kong) Co., Ltd.       Fax   : (00852)2559 3049
Received on Wed Jan 15 1997 - 00:00:00 CST

Original text of this message

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