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

Home -> Community -> Usenet -> c.d.o.server -> Re: How to find last record in a table?

Re: How to find last record in a table?

From: Noel <tbal_at_go2.pl>
Date: Thu, 15 Jan 2004 16:55:53 -0000
Message-ID: <bu6d2d$6fi$1@inews.gazeta.pl>

Użytkownik "Jim Kennedy" <kennedy-downwithspammersfamily_at_attbi.net> napisał w wiadomości news:3iyNb.59426$Rc4.215136_at_attbi_s54...
>
> > > The last record inserted, as in chronologically? Not possible, as
Oracle
> > does not support "natural" sort orders. You need to sequence your
> records,
> > add a numeric column named "seq" or something like that, then for each
> > insert assign it a value from a sequence generator. Then a simple query
> > like "select * from table where rownum=1 order by seq desc" will always
> give
> > you the last inserted record.
> >
> >
> No it won't. rownum is evaluated BEFORE the sort occurs.
> Jim

Thus, you need to sort first...

SELECT SEQ
    FROM
 (
 SELECT SEQ
     FROM TABLE
  ORDER BY SEQ DESC
)
WHERE ROWNUM=1; I would rather do:

SELECT *
     FROM TABLE
  WHERE SEQ =
    (
    SELECT MAX(SEQ)
        FROM TABLE
);

--
Noel
Received on Thu Jan 15 2004 - 10:55:53 CST

Original text of this message

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