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: <fitzjarrell_at_cox.net>
Date: Fri, 31 Aug 2007 09:22:46 -0700
Message-ID: <1188577366.396053.48920@o80g2000hse.googlegroups.com>


On Aug 31, 6:33 am, William Robertson <williamr2..._at_googlemail.com> wrote:
> 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;- Hide quoted text -
>
> - Show quoted text -

I should really get better at using the analytic functions.

Nice solutions.

David Fitzjarrell Received on Fri Aug 31 2007 - 11:22:46 CDT

Original text of this message

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