Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Only returning the last 20 rows from a table

Status
Not open for further replies.

clintonwhite

IS-IT--Management
Aug 8, 2002
42
0
0
GB
Hi There,

I have tried loking through the documentation for this one, but can not seem to find anything.

I have a table that contains hundreds of rows for different machines and I want to be able to select at most 20 rows out of the table, based upon the id of the required machine. I would like to retrieve the most recent 20 rows if possible, as there is a date column in the table - however I can not seem to do this, unless I retrieve all of the rows from the table for the required machine id, sorted by ascending date and them go last and count back for 20 rows.

Is anyone aware of any condition that I can use on the select statement to achieve the same result.

Thanks for you help.
 
Try:

[tt]SELECT * FROM (
SELECT *
FROM table
WHERE machine_id = <machine>
ORDER BY date_col DESC )
WHERE ROWNUM <= 20;[/tt]
 
Thats great - exactly what I want - thanks for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top