- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- only retrive LAST rown of sql query
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-15-2002 03:38 AM
тАО03-15-2002 03:38 AM
Can someone tell me how to only retrieve the LAST row of an sql query (equivalent of unix "tail -1 " command)
I have tried reversinq the order by clause and using "rownum = 1" but the ronwums are allocated before the order by
Any help much appreciated
Cheers
Ian
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-15-2002 03:50 AM
тАО03-15-2002 03:50 AM
Re: only retrive LAST rown of sql query
It's been a while, but something like
select field01, field02, field03
from your_table
where field01 = (select max(field01)
from your_table);
might work since you do seem to have fields you can order the table by.
Hope this helps,
Tom Geudens
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-15-2002 05:07 AM
тАО03-15-2002 05:07 AM
Re: only retrive LAST rown of sql query
If your query was something like:
select name, a_number
from mytab
order by a_number
and you want the last row returned from above, then you can change it to
select first 1 name, a_number
from mytab
order by a_number desc
This is informix syntax, don't know about oracle/sybase etc.
Otherwise you can do it the hard way (i.e. write a stored procedure or program to unload the rows, tail -1 on the file and read it back in returning one row)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-18-2002 12:54 AM
тАО03-18-2002 12:54 AM
SolutionIf you are talking Oracle...
As Steve says, without an 'order by' statement the order in which Oracle will retrieve your rows is indeterminate and may not be consistent between successive invocations of the same query.
If you *do* use 'order by' you can combine it
with "WHERE ROWNUM = nn"; ROWNUM is a pseudocolumn but as it is assigned as rows are retreived (ie before they are sorted according to 'order by') it still may not give you what you want.
In any case to retrieve the last row of a query is not so easy, you would have to do something like
select * from emp
where rownum = (select count(*) from emp)
Better to reverse the query and
select ..
where rownum = 1
Hope this helps. If you have access to MetaLink there is plenty more about rownum.
Graham
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-18-2002 01:31 AM
тАО03-18-2002 01:31 AM
Re: only retrive LAST rown of sql query
Of course, you'll have to reverse your Order-by clause as well.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-18-2002 03:13 AM
тАО03-18-2002 03:13 AM
Re: only retrive LAST rown of sql query
In relational database, there is no order in the extraction of rows, since it's based on algebric sets which do not support order between the constituants of these sets.
So, suppose that you are issuing a sql statement that matches 10 rows, the output order of these rows will be a random order and no matter to fix which one appear at last or at the begining.
Then, if you try to print only one row ( with special mechanism ) you will have a different result each time you run this sql statement while data didn't changed.
And this is completely false !
Conclusion : In relational algebra, these is no order in sets.
Hope this helps.
Magdi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-18-2002 08:19 AM
тАО03-18-2002 08:19 AM
Re: only retrive LAST rown of sql query
echo "set heading off pages 0 feed off heads off
select * from tab;" | sqlplus -s user/pass | tail -1
Substitute your SQL, of course.
Not a total Oracle solution, but works.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-22-2002 05:46 AM
тАО03-22-2002 05:46 AM
Re: only retrive LAST rown of sql query
if you can order on one of the fileds use this sql syntax
select ename,job from
(select ename,job from emp order by job,ename desc)
hwere rownum <=1;
that should retrieve the last record in the ordered output of the query.
Replace the column names and table name by what you need