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 427,774 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 3,712 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
Views: 5198 | Replies: 7
Reply
Join Date: Oct 2007
Posts: 5
Reputation: alidabiri is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
alidabiri alidabiri is offline Offline
Newbie Poster

how to select latest or last row in a table

  #1  
Oct 23rd, 2007
hi,
i have 2 tables: CUST and ORDERS
CUST contains cust_num, cust_name, cust_phone
ORDERS contains cust_num, order_date, order_amt
i want to list all the cust_names and their last order date and amount
how would i code this?
thanks.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2007
Location: Bangalore,India
Posts: 1,290
Reputation: debasisdas is on a distinguished road 
Rep Power: 4
Solved Threads: 83
debasisdas's Avatar
debasisdas debasisdas is offline Offline
Nearly a Posting Virtuoso

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

  #2  
Oct 23rd, 2007
Use join to retrive record from both the tables by joining on CUST_NUM field. then use group by.
Share your Knowledge.
Reply With Quote  
Join Date: Oct 2007
Posts: 5
Reputation: alidabiri is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
alidabiri alidabiri is offline Offline
Newbie Poster

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

  #3  
Oct 23rd, 2007
i know about join. how would the code look like, please?
Reply With Quote  
Join Date: Oct 2007
Posts: 5
Reputation: alidabiri is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
alidabiri alidabiri is offline Offline
Newbie Poster

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

  #4  
Oct 23rd, 2007
i want to list all the customer names and then latest date and amount for that date for every customer.
Reply With Quote  
Join Date: Oct 2007
Posts: 5
Reputation: alidabiri is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
alidabiri alidabiri is offline Offline
Newbie Poster

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

  #5  
Oct 23rd, 2007
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)
Reply With Quote  
Join Date: Dec 2007
Posts: 6
Reputation: darrinallen is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
darrinallen darrinallen is offline Offline
Newbie Poster

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

  #6  
Dec 16th, 2007
[quote=alidabiri;456278]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)[/QUOTE
]
Reply With Quote  
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

  #7  
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  
Join Date: Feb 2007
Location: Bangalore,India
Posts: 1,290
Reputation: debasisdas is on a distinguished road 
Rep Power: 4
Solved Threads: 83
debasisdas's Avatar
debasisdas debasisdas is offline Offline
Nearly a Posting Virtuoso

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

  #8  
Oct 24th, 2007
I am glad to know that you found the solution before i do that for you. Happy programming.
Share your Knowledge.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Oracle Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Oracle Forum

All times are GMT -4. The time now is 1:21 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC