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 > Data in Batches

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-12-09, 23:29
ratheeshknair ratheeshknair is offline
Registered User
 
Join Date: Jan 2009
Posts: 153
Data in Batches

Hi Experts,

How can i select data in batches?

Can we create a select query to fetch the rows from 10 to 20 as like the functionality of row_num() in oracle??

We should fetch the rows which we specify for the pagination.

Please Help

Thanks in Advance
Reply With Quote
  #2 (permalink)  
Old 11-13-09, 00:18
sawangupta sawangupta is offline
Registered User
 
Join Date: Nov 2009
Location: Bangalore
Posts: 25
Reply With Quote
  #3 (permalink)  
Old 11-13-09, 00:55
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
WITH T (EMP_NO, LAST_NAME, FIRST_NAME, rnum) AS
(SELECT EMPNO, LASTNAME, FIRSTNME, row_number() over(order by LASTNAME) FROM EMPLOYEE WHERE EDLEVEL > 12)
SELECT * FROM T where rnum BETWEEN 5 AND 10
__________________
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
  #4 (permalink)  
Old 11-15-09, 00:59
ratheeshknair ratheeshknair is offline
Registered User
 
Join Date: Jan 2009
Posts: 153
Thanks All,

Can i get the same in a single query? ie without the nested query.
Reply With Quote
  #5 (permalink)  
Old 11-15-09, 01:31
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
I don't know other ways without using nested query.

But, the common table expression can be replaced by a nested table expression, in this case.

WITH T(...) AS (SELECT ... )
SELECT * FROM T where rnum BETWEEN 5 AND 10
is equivalent to
SELECT * FROM (SELECT ... ) AS T(...) where rnum BETWEEN 5 AND 10
Reply With Quote
  #6 (permalink)  
Old 11-15-09, 19:47
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
I don't know other ways without using nested query.
Except the following self-join which is usually inefficient and not practical.
Code:
SELECT a.empno, a.lastname, a.firstnme
     , COUNT(b.lastname) AS rnum
  FROM employee a
     , employee b
 WHERE a.edlevel > 12
   AND b.edlevel > 12
   AND b.lastname <= a.lastname
 GROUP BY
       a.empno, a.lastname, a.firstnme
HAVING COUNT(b.lastname) BETWEEN 5 AND 10
 ORDER BY
       COUNT(b.lastname)
;
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