Unix Technical Forum

SQL to retrieve most recent row meeting criteria

This is a discussion on SQL to retrieve most recent row meeting criteria within the Oracle Database forums, part of the Database Server Software category; --> I've completed a port of my application to Oracle, SQL Server, MySQL and Teradata. That's been a learning effort ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-24-2008, 06:10 AM
Art Krumsee
 
Posts: n/a
Default SQL to retrieve most recent row meeting criteria

I've completed a port of my application to Oracle, SQL Server, MySQL and
Teradata. That's been a learning effort of considerable proportions. But I
do have an Oracle question.

I want to retrieve the most recent row from a table meeting a specific
criteria. In the example below, I want the most recent zipcode of the
individual with a specific email address. The table has an ID column which
increments automatically as rows are added. Right now I'm using:

select zip from journal where id=(select max(id) from journal where
email='xxx@yyy.com')

This works but, even with optimized indexing, it requires two hits of the
database to process. In SQL Server I was able to accomplish this in one hit
using their TOP syntax. Is there a more efficient way to accomplish this in
Oracle?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-24-2008, 06:10 AM
Mark D Powell
 
Posts: n/a
Default Re: SQL to retrieve most recent row meeting criteria

"Art Krumsee" <akrumsee@columbus.rr.com> wrote in message news:<21MLc.227249$DG4.49690@fe2.columbus.rr.com>. ..
> I've completed a port of my application to Oracle, SQL Server, MySQL and
> Teradata. That's been a learning effort of considerable proportions. But I
> do have an Oracle question.
>
> I want to retrieve the most recent row from a table meeting a specific
> criteria. In the example below, I want the most recent zipcode of the
> individual with a specific email address. The table has an ID column which
> increments automatically as rows are added. Right now I'm using:
>
> select zip from journal where id=(select max(id) from journal where
> email='xxx@yyy.com')
>
> This works but, even with optimized indexing, it requires two hits of the
> database to process. In SQL Server I was able to accomplish this in one hit
> using their TOP syntax. Is there a more efficient way to accomplish this in
> Oracle?


Art, normally I would expect the query to be written something like
select whatever from journal A
where email = 'xxxx@yyy.com'
and id = (select max(id) from journal B
where B.email = A.email )

The explan plan will show 2 accesses however the second access is just
to the buffers retrieved by the outer query.

HTH -- Mark D Powell --
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-24-2008, 06:11 AM
Richard Kuhler
 
Posts: n/a
Default Re: SQL to retrieve most recent row meeting criteria

Art Krumsee wrote:
> I've completed a port of my application to Oracle, SQL Server, MySQL and
> Teradata. That's been a learning effort of considerable proportions. But I
> do have an Oracle question.
>
> I want to retrieve the most recent row from a table meeting a specific
> criteria. In the example below, I want the most recent zipcode of the
> individual with a specific email address. The table has an ID column which
> increments automatically as rows are added. Right now I'm using:
>
> select zip from journal where id=(select max(id) from journal where
> email='xxx@yyy.com')
>
> This works but, even with optimized indexing, it requires two hits of the
> database to process. In SQL Server I was able to accomplish this in one hit
> using their TOP syntax. Is there a more efficient way to accomplish this in
> Oracle?


I won't make any claims regarding performance but analytics can do this ...

select *
from (
select last_value(zip) over (order by id)
from journal
where email = 'xxx@yyy.com'
)
where rownum = 1

--
Richard Kuhler



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-24-2008, 06:11 AM
Daniel Morgan
 
Posts: n/a
Default Re: SQL to retrieve most recent row meeting criteria

Art Krumsee wrote:

> I've completed a port of my application to Oracle, SQL Server, MySQL and
> Teradata. That's been a learning effort of considerable proportions. But I
> do have an Oracle question.
>
> I want to retrieve the most recent row from a table meeting a specific
> criteria. In the example below, I want the most recent zipcode of the
> individual with a specific email address. The table has an ID column which
> increments automatically as rows are added. Right now I'm using:
>
> select zip from journal where id=(select max(id) from journal where
> email='xxx@yyy.com')
>
> This works but, even with optimized indexing, it requires two hits of the
> database to process. In SQL Server I was able to accomplish this in one hit
> using their TOP syntax. Is there a more efficient way to accomplish this in
> Oracle?


Even in SQL Server you were hitting twice. They just hid one of the
hits.

That said ... if ID is indexed you aren't hitting the table anyway ...
you are hitting the index if the system is working properly.

Daniel Morgan

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-24-2008, 06:12 AM
Burt Peltier
 
Posts: n/a
Default Re: SQL to retrieve most recent row meeting criteria

All kinds of ways to do everything.... not saying this is the best, but here
is another option ...

select * from
(select zip from journal where email='xxx@yyy.com' order by id desc)
where rownum < 2

--

"Art Krumsee" <akrumsee@columbus.rr.com> wrote in message
news:21MLc.227249$DG4.49690@fe2.columbus.rr.com...
> I've completed a port of my application to Oracle, SQL Server, MySQL and
> Teradata. That's been a learning effort of considerable proportions. But

I
> do have an Oracle question.
>
> I want to retrieve the most recent row from a table meeting a specific
> criteria. In the example below, I want the most recent zipcode of the
> individual with a specific email address. The table has an ID column

which
> increments automatically as rows are added. Right now I'm using:
>
> select zip from journal where id=(select max(id) from journal where
> email='xxx@yyy.com')
>
> This works but, even with optimized indexing, it requires two hits of the
> database to process. In SQL Server I was able to accomplish this in one

hit
> using their TOP syntax. Is there a more efficient way to accomplish this

in
> Oracle?
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 09:49 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com