If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > limit in DB2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-17-11, 13:56
clockdva clockdva is offline
Registered User
 
Join Date: Jan 2011
Posts: 4
limit in DB2

i have a simmple query as

select * from my_table

i want put some row limit, for example from 20 to 30

in mysql is select * from my_table limit x,y

how it is in db2?

it's important that i want all columns, so i use *

thanks and sorry for my bad english
Reply With Quote
  #2 (permalink)  
Old 01-17-11, 14:55
schintala schintala is offline
Registered User
 
Join Date: Apr 2005
Location: USA
Posts: 119
How about using fetch first 20 rows only in SQL select statement.
Reply With Quote
  #3 (permalink)  
Old 01-17-11, 15:43
clockdva clockdva is offline
Registered User
 
Join Date: Jan 2011
Posts: 4
Quote:
Originally Posted by schintala View Post
How about using fetch first 20 rows only in SQL select statement.
it could also take up the rows from 41 to 60 (example) or juste the firts 20 (from 1 t 20)?

thanks
Reply With Quote
  #4 (permalink)  
Old 01-17-11, 16:01
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,198
I haven't tested this code, but should look something like this:
Code:
SELECT *
FROM ( SELECT
       ROWNUMBER() OVER(ORDER BY COL1) AS ROWNUMB,
       COL1,
       COL2,
       COL3,
       COL4
       FROM your_table) AS T
WHERE ROWNUMB BETWEEN 20 AND 30;
Please note that you should have an ORDER BY in the OVER clause for this to be meaningful, since without that you cannot guarantee the same results each time you run it. This is a fundamental principle of all relational databases.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #5 (permalink)  
Old 01-17-11, 16:04
clockdva clockdva is offline
Registered User
 
Join Date: Jan 2011
Posts: 4
Quote:
Originally Posted by Marcus_A View Post
I haven't tested this code, but should look something like this:
Code:
SELECT *
FROM ( SELECT
       ROWNUMBER() OVER(ORDER BY COL1) AS ROWNUMB,
       COL1,
       COL2,
       COL3,
       COL4
       FROM your_table) AS T
WHERE ROWNUMB BETWEEN 20 AND 30;
Please note that you should have an ORDER BY in the OVER clause for this to be meaningful, since without that you cannot guarantee the same results each time you run it. This is a fundamental principle of all relational databases.
i try tomorrow, now i'm at home

thanks
Reply With Quote
  #6 (permalink)  
Old 01-18-11, 04:27
clockdva clockdva is offline
Registered User
 
Join Date: Jan 2011
Posts: 4
ILLEGAL SYMBOL "(". SOME SYMBOLS THAT MIGHT BE LEGAL ARE: , FROM INTO SQL Code: -104, SQL State: 42601
Reply With Quote
  #7 (permalink)  
Old 01-18-11, 06:17
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
clockdva, Marcus_A did mention he was doing this without testing. There is a small typo in the query. It should be ROW_NUMBER() instead of ROWNUMBER()
Code:
WITH your_table (COL1, COL2, COL3, COL4)
  AS (SELECT 1, 'A', 'AA', 'AAA' FROM SYSIBM.SYSDUMMY1
      UNION ALL
      SELECT COL1 + 1, COL2, COL3, COL4 FROM your_table
      WHERE COL1 < 50
     )
SELECT *
FROM ( SELECT
       ROW_NUMBER() OVER(ORDER BY COL1) AS ROWNUMB,
       COL1,
       COL2,
       COL3,
       COL4
       FROM your_table) AS T
WHERE ROWNUMB BETWEEN 20 AND 30;
;

ROWNUMB              COL1        COL2 COL3 COL4
-------------------- ----------- ---- ---- ----
                  20          20 A    AA   AAA 
                  21          21 A    AA   AAA 
                  22          22 A    AA   AAA 
                  23          23 A    AA   AAA 
                  24          24 A    AA   AAA 
                  25          25 A    AA   AAA 
                  26          26 A    AA   AAA 
                  27          27 A    AA   AAA 
                  28          28 A    AA   AAA 
                  29          29 A    AA   AAA 
                  30          30 A    AA   AAA 

  11 record(s) selected.
Reply With Quote
  #8 (permalink)  
Old 01-18-11, 07:16
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
Quote:
... It should be ROW_NUMBER() instead of ROWNUMBER()
ROWNUMBER() may be valid.

IBM i DB2 for i SQL Reference 7.1
Quote:
Note: Syntax alternatives: DENSERANK can be specified in place of
DENSE_RANK, and ROWNUMBER can be specified in place of
ROW_NUMBER.
DB2 Version 9.1 for z/OS SQL Reference
Quote:
Syntax alternatives and synonyms: For compatibility, the keywords DENSERANK
and ROWNUMBER can be used as synonyms for DENSE_RANK and
ROW_NUMBER respectively.
IBM DB2 9.7 for Linux, UNIX, and Windows SQL Reference, Volume 1
Quote:
The ROW_NUMBER (or ROWNUMBER) function computes the sequential row
number of the row within the window defined by the ordering, starting with 1
for the first row.
Reply With Quote
  #9 (permalink)  
Old 01-18-11, 07:22
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
Thanks for the correction, tonkuma. I went straight to the examples and missed that part of the manual.

clockdva, if you could post your SQL with the error, someone may be able to find the problem (if you can't).
Reply With Quote
  #10 (permalink)  
Old 01-18-11, 07:58
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,195
I thought clockdva might be using older version of DB2 on which OLAP specifications were not supported.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On