Solved

DB2 - Select x number of rows

Posted on 1998-12-02
10
14,225 Views
Last Modified: 2012-05-04
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
Comment
Question by:mmoore
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 2

Expert Comment

by:odessa
ID: 1026049
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
 
LVL 3

Author Comment

by:mmoore
ID: 1026050
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
 
LVL 7

Expert Comment

by:Dedushka
ID: 1026051
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
 
LVL 3

Author Comment

by:mmoore
ID: 1026052
Sorry, that dies not work. Gets "Unexpected token" when it
encounters the "50".

0
 
LVL 2

Expert Comment

by:odessa
ID: 1026053
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 7

Expert Comment

by:Dedushka
ID: 1026054
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
 
LVL 3

Author Comment

by:mmoore
ID: 1026055
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
 
LVL 3

Accepted Solution

by:
P_S_Price earned 50 total points
ID: 1026056
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
 
LVL 3

Author Comment

by:mmoore
ID: 1026057
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
 

Expert Comment

by:N8DaGr8
ID: 7321914
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now