Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Retrieving first row of a query in Oracle SQL

Re: Retrieving first row of a query in Oracle SQL

From: William Robertson <williamr2019_at_googlemail.com>
Date: Fri, 31 Aug 2007 04:33:09 -0700
Message-ID: <1188559989.862083.302750@22g2000hsm.googlegroups.com>


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 )

WHERE seq = 1;

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
FROM t; Received on Fri Aug 31 2007 - 06:33:09 CDT

Original text of this message

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