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

LIMIT ROWS RETRIEVED USING SQL 1

Status
Not open for further replies.

Queryman

Programmer
Joined
Nov 4, 2002
Messages
243
Location
US
Could someone give an example on how to limit rows retrieved (ANSI SQL)
Thanks!

QueryMan

 
In SQL:1999 there's a ROW_NUMBER function, but it's not yet included (you'll have to wait until V2R5 in December).
But you can achieve the same result using
select * from table
qualify sum(1) over (order by col
rows unbounded preceding) <= 10

or
qualify rank() over (order by col) <= 10

As QUALIFY is no standard SQL you'll have to use a derived table to be ANSI compliant:

select ... from
(select ..., rank() over (order by col) AS rownum
from table) dt
where rownum <= 10

If you don't care about the order (and about ANSI SQL) use a SAMPLE clause, this will be the fastest way:
sel * from table sample 10

Dieter
 
Excellent, Thanks!

QueryMan

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top