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 Join in SQL Query vickey_dw DB2 2 Tue Jan 17, 2017 12:15 am
This topic is locked: you cannot edit posts or make replies. MQ setup query mohitsaini Java & MQSeries 4 Sun Jan 08, 2017 1:53 am
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am
This topic is locked: you cannot edit posts or make replies. Db2 SQL query to convert rows into co... subratarec DB2 5 Thu Nov 17, 2016 4:51 pm


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