User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Oracle section within the Web Development category of DaniWeb, a massive community of 402,084 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,504 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Oracle advertiser: Programming Forums

how to select latest or last row in a table

Join Date: May 2007
Posts: 22
Reputation: markchicobaby is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
markchicobaby markchicobaby is offline Offline
Newbie Poster

Re: how to select latest or last row in a table

  #8  
Mar 10th, 2008
here's the correct answer that i figured out:
SELECT cu.cust_name,
            od.order_amount
FROM customers cu, 
         orders od
WHERE cu.cust_id = od.cust_id 
  and od.order_date =
 (select max(order_date) from orders od
        where od.cust_id = cu.cust_id)

That's not a great way to do it, because you've got a subquery. Doing two queries when only one is needed.

You'll find it should perform faster if you do a join and then get the max all in one queery, something like below. It depends on how Oracle optimises it though, performance issues can be fun to play with. In general the following will be better than what you've got above:

select cu.cust_name, od.order_amount, max(order_date) 
  from customers cu
  join  orders od
    on (cu.cust_id = od.cust_id)
group by cu.cust_name, od.order_amount

Having said that, it really does depend on a number of factors which I won't get into for now.... anyway you have some options now
Last edited by peter_budo : Mar 10th, 2008 at 8:23 pm. Reason: Keep It Organized - please use [code] tags
Reply With Quote  
All times are GMT -4. The time now is 1:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC