Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Limitting result without ROWNUM
Charles Hooper wrote:
> Matthias Matker wrote:
> > Vladimir M. Zakharychev schrieb:
> > >> I have to limit the number of results without using ROWNUM or something
> > >> like that. One hint was to use "JOIN".
> > >>
> > >> I have to select the 10 biggest persons from a table "persons"
> > >>
> > >> id, firstname, lastname, size, age
> > >>
> > >>
> > >> WITHOUT using ROWNUM or "limitter" like this.
> > >>
> > >> Do you have any idea?
> > >>
> > >> Thanks a lot.
> > >
> > > Strange requirement - care to explain why?
> >
> > It forbidden to use such "limiters", we ( students) should do this via a
> > kind of "JOIN". We MUST do so.
> > I have tried it for hours...
> >
> > > I'd suggest using RANK() or
> > > DENSE_RANK() analytic functions for this, but you might consider them
> > > "limiters", too, so...
> >
> > That's right ;-)
> >
> > > How about just fetching the first 10 rows of the
> > > result set and ignoring the rest?
> >
> > Cannot act like this.
> >
> > Thanks for your quick reply.
>
>
>
>
>
> The results of the above methods look something like this:
> ID FIRSTNAME LASTNAME HEIGHT AGE
> ---------------------------------------
> 2 SUSAN SMITH 65 20
> 3 DOROTHY SMITH 62 21
> 4 JOHN SMITH 72 35
> 5 DAVID SMITH 73 34
> 7 ROBERT SMITH 76 45
> 10 SUSAN JOHNSON 65.5 20
> 11 DOROTHY JOHNSON 62.5 21
> 12 JOHN JOHNSON 72.5 35
> 13 DAVID JOHNSON 73.5 34
> 15 ROBERT JOHNSON 79 45
>
>
Method #4:
SELECT
ID,
FIRSTNAME,
LASTNAME,
HEIGHT,
AGE
FROM
(SELECT
ID,
FIRSTNAME,
LASTNAME,
HEIGHT,
AGE,
(FIRST_VALUE(HEIGHT) OVER (ORDER BY HEIGHT DESC))*HEIGHT MY_PROD,
MAX_PROD
FROM
(SELECT
MAX(PROD) MAX_PROD
FROM
(SELECT HEIGHT*LEAD(HEIGHT,9,0) OVER (ORDER BY HEIGHT DESC) PROD FROM T1)),
Method #6:
SELECT
ID,
FIRSTNAME,
LASTNAME,
HEIGHT,
AGE
FROM
(SELECT
RANK() OVER (ORDER BY HEIGHT)/COUNT(*) OVER (PARTITION BY 1)
ROW_PERCENT,
(COUNT(*) OVER (PARTITION BY 1)-10)/COUNT(*) OVER (PARTITION BY 1)
CUT_OFF,
ID,
FIRSTNAME,
LASTNAME,
HEIGHT,
AGE
FROM
T1)
WHERE
ROW_PERCENT>CUT_OFF;
Method #7:
SELECT
T1.ID, T1.FIRSTNAME, T1.LASTNAME, T1.HEIGHT, T1.AGE
Method #8:
SELECT
ID,
FIRSTNAME,
LASTNAME,
HEIGHT,
AGE
FROM
(SELECT
ID,
FIRSTNAME,
LASTNAME,
HEIGHT,
AGE
FROM
T1
ORDER BY
HEIGHT DESC)
WHERE
ROWNUM<=10;
I guess the one above is disqualified, since it uses ROWNUM and is reasonably efficient.
Charles Hooper
PC Support Specialist
K&M Machine-Fabricating, Inc
Received on Sat Jan 20 2007 - 07:24:06 CST
![]() |
![]() |