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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS SQL equivalent of TOP n

Status
Not open for further replies.

holdem

Programmer
Apr 4, 2003
2
GB
I am new to DB2. Is there an equivalent to TOP n in MS SQL, to only return the TOP n records of a resultset?
 
Thanks, But where do I find info on row_number. I did a search at IBM's info center and looked through the on-line SQL reference there and couldn't find it. Do you have a suggested source for DB2 SQL reference? I am 2 weeks into DB2 and need to build up my reference links and books.

Thanks
 
holdem,

you will find the direct equivalent of TOP n is FETCH FIRST n ROWS.

Example

Select Cust_No, Order_No, Order_Qty
FROM
ORDER_TABLE
FETCH FIRST 10 ROWS ONLY.

Using the above predicate will ensure you only retrieve 10 rows.

Cheers
Greg
 
Even more flexible is the use of the rank() or dense_rank()function , if you combine it with the 'partition over' phrase. This will allow you to rank data first and then limit the resultset by setting condition on the individual ranks.

rank() over (order by <field1>)

rank() over (partition by <field2> order by <field1>) T. Blom
Information analyst
tbl@shimano-eu.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top