IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Fetch nth maximum record using DB2 query


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
akshaiya

New User


Joined: 17 Sep 2005
Posts: 49

PostPosted: Tue Mar 06, 2007 5:16 am
Reply with quote

I want to fetch nth maximum record using DB2 query in the cobol program.

Input File :

s-num ind pt-cd ssn

5353456 y t
5353456 y t
5353456 y t
6553452 y g
6553452 y g

Table :

s-num s1-num(CHAR 3) s2-num (CHAR 3)
5353456 999 876
5353456 999 875
5353456 999 874
5353456 999 873
5353456 999 872
6553452 AA 103
6553452 AA 102
6553452 AA 101

Output file :

s-num ind pt-cd ssn SSN=(s1-num appended with s2-num)

5353456 y t 999876
5353456 y t 999875
5353456 y t 999874
6553452 y g AA103
6553452 y g AA103

First record should fetch first maximum and the second record should fetch second maximum and nth record should fetch nth maximum.

Is it possible with the query?
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Tue Mar 06, 2007 8:52 am
Reply with quote

Hello,

It would be fairly simple if you use a cursor.

Is there any reason that you cannot use a cursor?
Back to top
View user's profile Send private message
vini_srcna

Active User


Joined: 26 May 2005
Posts: 178
Location: Copenhagen, Denmark

PostPosted: Tue Mar 06, 2007 3:11 pm
Reply with quote

I think he is talking about the select statement to put in the DECLARE cursor. I guess he is using cursor as he is talking about FETCH & COBOL.

You can do that using the ORDER BY.

EXEC SQL DECLARE CUR1 CURSOR FOR
SELECT s-num,ind, pt-cd, s1-num||s2-num FROM TABLE NAME
WHERE condition
ORDER BY 1,2,3,4.

The fetch of this cursor in the cobol program should give the results you are expecting. Let me know if this doesnt work. Thanks
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts How to split large record length file... DFSORT/ICETOOL 10
No new posts SFTP Issue - destination file record ... All Other Mainframe Topics 2
No new posts FINDREP - Only first record from give... DFSORT/ICETOOL 3
No new posts To find whether record count are true... DFSORT/ICETOOL 6
No new posts RC query -Time column CA Products 3
Search our Forums:

Back to Top