• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 14260
  • Last Modified:

DB2 - Select x number of rows

How can I limit a select statement to x numbers of
rows returned. If this was Oracle I could say
select * from my_table where ROWNUM < 50
to get the first 49 rows. I want to do the same
thing with DB2.
0
mmoore
Asked:
mmoore
  • 4
  • 2
  • 2
  • +2
1 Solution
 
odessaCommented:
In fact one way is by recursion

WITH B (CKEY,LVL)
(SELECT DISTINCT PKEY, 0
 FROM TROUBLE
 WHERE PKEY = 0
 UNION ALL
 SELECT C.CKEY, P.LVL +1
 FROM TROUBLE  
 WHERE PKEY NOT IN (SELECT CKEY FROM PARENT)
)
SELECT CKEY, LVL
FROM PARENT
WHERE LVL < 50;

HOPE it's helpful
0
 
mmooreAuthor Commented:
Wow, that is really wild! I don't quite understand what is happening. Could you
show me the example with table ORDER  and columns ORD_NUM and CUST_ID?
This would help me to see what is table data and what is IBM SQL
reserved words.
What is "WITH B (CKEY,LVL) "? Is this part of the syntax I would type in or is
it part of your explanation?
I am trying to use this with IBM's JDBC driver. I wonder if that will make a difference.
Thanks, this will be really helpful if it works!
Mike

0
 
DedushkaCommented:
Hi, mmoore!

Try to use keyword "TOP" in you query, i.e.

SELECT TOP 50 MY_TABLE.*
FROM MY_TABLE;

Happy New Year!
Dedushka
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
mmooreAuthor Commented:
Sorry, that dies not work. Gets "Unexpected token" when it
encounters the "50".

0
 
odessaCommented:
No problem
WITH B (CKEY,LVL)
(SELECT DISTINCT PKEY, 0
 FROM TROUBLE
 WHERE PKEY = 0
 UNION ALL
 SELECT C.CKEY, P.LVL +1
 FROM TROUBLE  
 WHERE PKEY NOT IN (SELECT CKEY FROM PARENT)
)
SELECT CKEY, LVL , ORD_NUM, CUST_ID
FROM PARENT
WHERE LVL < 50;
ORDER BY ORD_NUM, CUST_ID

Now this is a recursion generated query "WITH B (CKEY,LVL) " is definition of temp Table B for internal use of recursion, about ODBC driver I don't now how it works
0
 
DedushkaCommented:
Hi, mmoore!

Standard ANSI SQL include the LIMIT TO nn ROWS clause used to limit the number of rows returned by a query. Is DB2 support this standard?

Best regards.
Dedushka
0
 
mmooreAuthor Commented:
Hi again Dedushka!
I do not know if DB2 supports that standard.
I tried:
select * from iprov.rld_feature
and that works fine, so next I tried
select * from iprov.rld_feature limit to 10 rows
the result was " ... unexpected token ... "

Best Wishes,
Mike

0
 
P_S_PriceCommented:
I have checked the DB2 Manuals for AS/400 and the IBM Implementation of SQL does not support 'TOP' or 'LIMIT'. The only way I can think that you can do this is to define a cursor for the select and then count the first N records retrieved in a program by the using the cursor.
0
 
mmooreAuthor Commented:
I can not really create a cursor because this is an ad-hoc SELECT statement using the JDBC interface. However I think that your answer about there being no built-in SQL syntax to accomplish the task is correct.
0
 
N8DaGr8Commented:
Noticing the date on the original question, this might not have been a valid answer at the time, however for anyone who currently looks to this question in order to answer their current questions, it would be nice to post the correct answer.

In DB2 use a fetch first x rows only

So the query would be...
Select * from tablename where row='123' fetch first 50 rows only

Note, you can also say
Select * from tablename where row='123' fetch first 50 row only
Select * from tablename where row='123' fetch first 1 row only
Select * from tablename where row='123' fetch first 1 rows only
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now