Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Fetch nth maximum record using DB2 query

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: Fetch nth maximum record using DB2 query
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

Site Director


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

PostPosted: Tue Mar 06, 2007 8:52 am    Post subject:
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    Post subject:
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Need to write record of PS File in ex... Chandan1993 JCL & VSAM 1 Wed Jun 07, 2017 1:35 am
No new posts Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am
No new posts Adding big TEXT lines to each record ... bshkris SYNCSORT 4 Sat May 06, 2017 1:40 am
This topic is locked: you cannot edit posts or make replies. Merge two files and update Trailer re... Yashashri JCL & VSAM 1 Thu May 04, 2017 12:54 pm
No new posts Sort Large record length cmsmoon DFSORT/ICETOOL 14 Tue Apr 11, 2017 5:49 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us