473,322 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

SQL on DB2 - restrict number of rows returned

In Oracle you can restrict the number of rows returned by specifying 'WHERE
ROWNUM < 10'. Can you do this in DB2 as well?

Nov 12 '05 #1
2 6607
AnnieLab wrote:
In Oracle you can restrict the number of rows returned by specifying
'WHERE
ROWNUM < 10'. Can you do this in DB2 as well?


SELECT ... FROM ... WHERE ... FETCH FIRST 10 ROWS ONLY

Or use the ROW_NUMBER OLAP function:

SELECT ...
FROM ( SELECT ..., row_number() over() AS rn
FROM ...
WHERE ... ) AS t
WHERE rn < 10

Or use a UDF like the "counter" function available in the samples:

SELECT ...
FROM ...
WHERE counter() < 10

--
Knut Stolze
Information Integration Development
IBM Germany / University of Jena
Nov 12 '05 #2
Hi,

1. using FETCH FIRST clause
SELECT * FROM Table FETCH FIRST 10 ROWS ONLY

2. using rownumber OLAP function
SELECT * FROM (
SELECT col1,
rownumber() over(ORDER BY col1) AS rn
FROM Table) AS t
WHERE t.rn <=10

Regards
Adrian

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris E | last post by:
Hi, I need to print a message when the number of rows returned from repeating groups is > 3. How is this best done? I have two repeating groups filtered to display only 3 rows. When either one...
6
by: yoohanman | last post by:
Hi, I have a select statment that correctly returns zero rows at times. I would like to be able to return the value 0 (a single row with the value 0) whenever the logic returns zero rows. ...
7
by: Egor Shipovalov | last post by:
I'm implementing paging through search results using cursors. Is there a better way to know total number of rows under a cursor than running a separate COUNT(*) query? I think PostgreSQL is bound...
1
by: BF | last post by:
Hello, I am trying to find out if there is a way to limit the number of rows returned when a cursor is opened. I am using DB2 version 7 on z/OS. The SELECT statement in my DECLARE CURSOR...
3
by: Juan | last post by:
how to get number of rows returned by a SqlDataReader without having to scroll it?
4
by: Chris Tremblay | last post by:
I am trying to figure out how to go about retrieving the number of results returned from my queries in SQL server from VB.NET without using a the Select Count(*) query. The method that I was using...
0
by: John A Grandy | last post by:
In ASP.NET 2.0 , does the ObjectDataSource provide a method or property to return the number of rows returned ?
4
by: esmith2112 | last post by:
I have a situation that I can't explain. Boiled down to its essence, I have a query of the form SELECT A.COL1, A.COL2, B.COL1 FROM A LEFT JOIN B ON A.KEY = B.KEY This query produces 5383...
2
by: yuva17raj | last post by:
hi i am using the select query with group by, so that i am getting records with more than one column so that i need to calculate the number of rows returned by the query for example with my...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.