Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: getting multiple Newest records
jb <jbecker_at_fhcrc.org.righteo> wrote in news:3E371429.8050005
@fhcrc.org.righteo:
> Steve Wicklund wrote: >
> SQL> select myid, descrip, d_event > 2 from yada > 3 where (myid, d_event) in > 4 (select myid, max(d_event) from yada group by myid); > > MYID DESCRIP D_EVENT > --------- ---------------------------------------- ---------- > 1 quit 01/21/2003 > 2 ordered 01/12/2003 > 3 registered 01/16/2003 > > someone has a faster way i'm open to that. > > cheers > jrb
If you have 8i (I think) you can use the analytic function row_number and an in line view which is quite fast.
SQL> select * from t;
ROWID ID DESCRIPTION ID_DATE
----- -- ------------ --------- row1 01 yada 10-JAN-03 row2 02 yada2 10-JAN-03 row3 01 newYada 12-JAN-03 row4 02 yada3 20-JAN-03 row5 03 yadayada 20-FEB-03 row5 04 yada5 01-FEB-03
6 rows selected.
SQL> select * from (
2 select row_id, id, description, id_date, 3 row_number() over (partition by id order by id_date desc) x 4 from t)
ROWID ID DESCRIPTION ID_DATE X
----- -- ------------ --------- ---------- row3 01 newYada 12-JAN-03 1 row4 02 yada3 20-JAN-03 1 row5 03 yadayada 20-FEB-03 1 row5 04 yada5 01-FEB-03 1
If you want to use them in PL/SQL you need to use native dynamic SQL until 9i though.
Martin Received on Tue Jan 28 2003 - 19:11:50 CST
![]() |
![]() |