| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
|  |  | |||
Home -> Community -> Usenet -> c.d.o.misc -> Re: Retrieving first row of a query in Oracle SQL
On Aug 30, 2:39 pm, "fitzjarr..._at_cox.net" <fitzjarr..._at_cox.net> wrote:
> On Aug 30, 1:30 am, digory <dig..._at_gmx.net> wrote:
>
> > >   SELECT id, dat, cnt, stuff
> > >    FROM T
> > >    WHERE (cnt,dat) IN (
> > >       SELECT MIN (cnt), MIN(dat)
> > >       FROM T
> > >       WHERE dat = (
> > >          SELECT MIN(dat)
> > >          FROM T
> > >       )
> > >    )
>
> > That isn't simpler either. Actually, the innermost WHERE clause is
> > redundant, isn't it?
>
> No, since  you declared that the COMBINATION of the two values is
> unique, and the only way you'll get that is to include the min(dat) in
> the select list.
>
> David Fitzjarrell
One way:
SELECT id, dat, cnt, stuff
FROM   ( SELECT id, dat, cnt, stuff
              , ROW_NUMBER() OVER (ORDER BY dat ASC, cnt ASC) AS seq
         FROM   t )
Another (although handling the BLOB column requires a bit of a hack so I'm not sure it's a good idea in this particular case):
SELECT MIN(id)
     , MIN(dat) KEEP (DENSE_RANK FIRST ORDER BY dat ASC, cnt ASC) dat
     , MIN(cnt) KEEP (DENSE_RANK FIRST ORDER BY dat ASC, cnt ASC) cnt
     , MIN(DBMS_LOB.SUBSTR(stuff,4000,1)) KEEP (DENSE_RANK FIRST ORDER
BY dat ASC, cnt ASC) stuff
|  |  |