| | |
how to select latest or last row in a table
Please support our Oracle advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: May 2007
Posts: 26
Reputation:
Solved Threads: 0
here's the correct answer that i figured out:
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:
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
Oracle Syntax (Toggle Plain Text)
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:
Oracle Syntax (Toggle Plain Text)
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 9:23 pm. Reason: Keep It Organized - please use [code] tags
•
•
Join Date: Jan 2009
Posts: 2
Reputation:
Solved Threads: 0
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?
In this scenario, how to retrieve the last row?
•
•
Join Date: Jan 2009
Posts: 2
Reputation:
Solved Threads: 0
The query would be :
[this is just a sample query]
[this is just a sample query]
sql Syntax (Toggle Plain Text)
SELECT empno,sal FROM(SELECT a.*, rownum rwnm FROM(SELECT empno,sal FROM test ORDER BY rownum desc)a 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.
![]() |
Similar Threads
- How to select the Last row from a table..using Mysql (MySQL)
- mysql_fetch_array(): supplied argument is not a valid MySQL (PHP)
- AJAX generated <select> and FIREFOX (JavaScript / DHTML / AJAX)
- can i select the 2nd row/record from a table (MySQL)
- javascript to add select button (JavaScript / DHTML / AJAX)
- ASP .NET database hit counter (ASP.NET)
- Find and Replace (Database Design)
Other Threads in the Oracle Forum
- Previous Thread: discoverer pus running totals
- Next Thread: Accessing Oracle 9i thru Java
| Thread Tools | Search this Thread |
Tag cloud for Oracle
2009predictions acquisition amazon.com bartz bernanke cia citrix cloudcomputing crm database dell economy editor enterprise enterprise2.0 enterprisesoftware erp federalreserve forbes hp ibm intellipedia internet larryellison layoffs linux loughridge mediawiki michaeljackson microsoft neverland nortel notebooks oil operatingsystem oracle palm rimm saas salesforce sap seagate socialcomputing sql sun sybase technologystocks virtualiron virtualization vmware wiki wikipedia xen yahoo zoho






