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

how to select n max value from column


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

New User


Joined: 24 Nov 2004
Posts: 20
Location: chennai

PostPosted: Thu Nov 25, 2004 10:27 am
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
jz1b0c

Active User


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

PostPosted: Thu Nov 25, 2004 10:36 pm
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

New User


Joined: 30 Jul 2004
Posts: 95

PostPosted: Fri Nov 26, 2004 7:19 pm
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: 160
Location: Toronto, Canada

PostPosted: Sat Nov 27, 2004 3:13 am
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: 160
Location: Toronto, Canada

PostPosted: Sat Nov 27, 2004 3:48 am
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
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 Replacing 'YYMMDD' with date, varying... SYNCSORT 3
No new posts How to load to DB2 with column level ... DB2 6
No new posts RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts first column truncated in search result IBM Tools 13
Search our Forums:

Back to Top