Portal | IBM Manuals | Downloads | Products | Refer | Info | Programs | JCLs | Forum Rules*| Site Map | Mainframe CD 
IBMMAINFRAMES.com - IBM Mainframe Support Forums Index
 
Register
 
IBMMAINFRAMES.com - IBM Mainframe Support Forums Index FAQ Search Memberlist Usergroups Profile Log in to check your private messages Log in
 
how to select n max value from column

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
Author Message
veera
Warnings : 1

New User


Joined: 24 Nov 2004
Posts: 21
Location: chennai

PostPosted: Thu Nov 25, 2004 10:27 am    Post subject: how to select n max value from column
Reply with quote

[size=9][color=red]hello, friends
1. how to select first n max values from the perticular column.
plse give me query.
2. give me out rec syntax.


veerababu.
Back to top
View user's profile Send private message
References
jz1b0c

Active User


Joined: 25 Jan 2004
Posts: 180
Location: Toronto, Canada

PostPosted: Thu Nov 25, 2004 10:36 pm    Post subject: Re: how to select n max value from column
Reply with quote

veera wrote:
[size=9][color=red]hello, friends
1. how to select first n max values from the perticular column.
plse give me query.
2. give me out rec syntax.


veerababu.


VEERA, HERE IS THE ANSWER


SELECT "LOAN_NUM"
FROM TEST.LOAN
ORDER BY LOAN_NUM ASC
FETCH FIRST 4 ROWS ONLY ;

IF YOU ARE USING THIS IN A DB2 PROGRAM
USE OPTIMIZE CLAUSE IN DECLARE STATEMENT.
Back to top
View user's profile Send private message
ksivapradeep

Active User


Joined: 30 Jul 2004
Posts: 98

PostPosted: Fri Nov 26, 2004 7:19 pm    Post subject: Re: how to select n max value from column
Reply with quote

hi jz1b0c,

veera asked the first height rows not the first n rows how to write that.ur query is correct but thats only to fetch first n rows.

regards,
siva pradeep
Back to top
View user's profile Send private message
jz1b0c

Active User


Joined: 25 Jan 2004
Posts: 180
Location: Toronto, Canada

PostPosted: Sat Nov 27, 2004 3:13 am    Post subject: Re: how to select n max value from column
Reply with quote

veera wrote:
[size=9][color=red]hello, friends
1. how to select first n max values from the perticular column.
plse give me query.
2. give me out rec syntax.


veerababu.


" first n max values " This confused me. It should have been nth max value in a table.
Back to top
View user's profile Send private message
jz1b0c

Active User


Joined: 25 Jan 2004
Posts: 180
Location: Toronto, Canada

PostPosted: Sat Nov 27, 2004 3:48 am    Post subject:
Reply with quote

Veera,

Try this.

SELECT *
FROM TEST.TABLE1 T1
WHERE 5 = ( SELECT COUNT(DISTINCT T2.NUM)
FROM TEST.TABLE2 T2
WHERE T2.NUM >= T1.NUM)
ORDER BY NUM DESC ;;


tHIS WILL GIVE 5TH MAX
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1