Re: SELECTing first x results (a FAQ?)
Posted in 1998
Taking from Joe Celko's book "SQL FOR SMARTIES", you can try something like
this (assuming column x is your 'ranking' column):
Select t1.a, t1.b, t1.c, t1.d, t1.x, count(*)
from mytable t1, mytable t2
where t1.x <= t2.x
group by t1.x
having count(*) <=50;
Granted, this will not perform the greatest, but it is a 'SQL-only' solution
D. Sanderson wrote:
> Despite the fact that this is probably a common question, all the usual
> sources (Informix manuals, Informix and SQL FAQs, DejaNews) came up empty.
> (I apologize ahead of time, particularly because I'm pretty sure I *did*
> see this in a manual somewhere, but I just can't find it now.)
>
> I want an SELECT statement that returns the first x results (x being a
> constant, say, 50) matching certain criteria. (I want an exact number, so
> I can't use the 'fraction of' formula in the FAQ.)
>
> Our client uses a port (just one) to get to the database, and I don't want
> a broad user-specified search to tie up the port getting hundreds of
> results.
>
> Many thanks for any assistance you can provide!
>
> _____________________________________________________________________
> Dan Sanderson dsanders@u.washington.edu
> University of Washington http://weber.u.washington.edu/~dsanders