how to select latest or last row in a table

Reply

Join Date: Oct 2007
Posts: 5
Reputation: alidabiri is an unknown quantity at this point 
Solved Threads: 0
alidabiri alidabiri is offline Offline
Newbie Poster

how to select latest or last row in a table

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 2,134
Reputation: debasisdas will become famous soon enough debasisdas will become famous soon enough 
Solved Threads: 132
debasisdas's Avatar
debasisdas debasisdas is offline Offline
Postaholic

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

 
0
  #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 Quick reply to this message  
Join Date: Oct 2007
Posts: 5
Reputation: alidabiri is an unknown quantity at this point 
Solved Threads: 0
alidabiri alidabiri is offline Offline
Newbie Poster

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

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

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

 
0
  #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 Quick reply to this message  
Join Date: Oct 2007
Posts: 5
Reputation: alidabiri is an unknown quantity at this point 
Solved Threads: 0
alidabiri alidabiri is offline Offline
Newbie Poster

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

 
1
  #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 Quick reply to this message  
Join Date: Feb 2007
Posts: 2,134
Reputation: debasisdas will become famous soon enough debasisdas will become famous soon enough 
Solved Threads: 132
debasisdas's Avatar
debasisdas debasisdas is offline Offline
Postaholic

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

 
0
  #6
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 Quick reply to this message  
Join Date: Dec 2007
Posts: 6
Reputation: darrinallen is an unknown quantity at this point 
Solved Threads: 0
darrinallen darrinallen is offline Offline
Newbie Poster

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

 
0
  #7
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 Quick reply to this message  
Join Date: May 2007
Posts: 26
Reputation: markchicobaby is an unknown quantity at this point 
Solved Threads: 0
markchicobaby markchicobaby is offline Offline
Light Poster

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

 
0
  #8
Mar 10th, 2008
here's the correct answer that i figured out:
  1. SELECT cu.cust_name,
  2. od.order_amount
  3. FROM customers cu,
  4. orders od
  5. WHERE cu.cust_id = od.cust_id
  6. AND od.order_date =
  7. (SELECT MAX(order_date) FROM orders od
  8. 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:

  1. SELECT cu.cust_name, od.order_amount, MAX(order_date)
  2. FROM customers cu
  3. join orders od
  4. ON (cu.cust_id = od.cust_id)
  5. 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 9:23 pm. Reason: Keep It Organized - please use [code] tags
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: ishii is an unknown quantity at this point 
Solved Threads: 0
ishii ishii is offline Offline
Newbie Poster

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

 
0
  #9
Jan 5th, 2009
In query, we are selecting the date as max date using max function but suppose if the last inserted row is the date less than the existing dates [it means this is the last record we have inserted in table] & if we want to retrieve this row...then max function would fail.
In this scenario, how to retrieve the last row?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: ishii is an unknown quantity at this point 
Solved Threads: 0
ishii ishii is offline Offline
Newbie Poster

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

 
0
  #10
Jan 6th, 2009
The query would be :
[this is just a sample query]

  1. SELECT empno,sal
  2. FROM(SELECT a.*, rownum rwnm FROM(SELECT empno,sal FROM test
  3. ORDER BY rownum desc)a
  4. WHERE rownum = 1)
Last edited by peter_budo; Jan 6th, 2009 at 9:11 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Oracle
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC