Re: [Q] Daves's top 10 SQL question

From: Austin Moseley <\"moseba_at_audv55.aud.alcatel.com'>
Date: 1996/03/23
Message-ID: <4ivshp$dh_at_news01.aud.alcatel.com>#1/1


Carlos Augusto Leite Netto <cnetto_at_cps.softex.br> wrote:
>Chuck Hamilton wrote:
>> >>But seriously, how can you code a SQL query to first order records
>> >>greatest to least then pick out only a pre-selected number of them
>> >>without prior knowledge of their distribution.
>
>select your_value
>from your_table o
>where 10 > (
> select count(*)
> from your_table i
> where i.value > o.value );

Better, yet:

 select rownum, value from ( select value from table group by value )

    where rownum < K;

This is fast. Obviously, it breaks down for most other logical operators, though.

-Austin Moseley

"0 + 1 = 0" Received on Sat Mar 23 1996 - 00:00:00 CET

Original text of this message