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

how to get third largest element in a table?


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

New User


Joined: 31 Jan 2007
Posts: 56
Location: banglore

PostPosted: Sat May 19, 2007 1:17 pm
Reply with quote

hi,

can any one please tell me how to get third largest element in a table
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: Sat May 19, 2007 7:33 pm
Reply with quote

Hello,

Set up a cursor to SELECT, ORDER BY DESC, and then use the 3rd result fetched.
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Sun May 20, 2007 8:03 am
Reply with quote

krishna_mf wrote:
hi,

can any one please tell me how to get third largest element in a table


Code:
SELECT MAX(ELEMENT) FROM TABLE
WHERE ELEMENT < (SELECT MAX(ELEMENT) FROM TABLE)
 


That will get you the second largest element, you should be able to expand that to get the third largest.
Back to top
View user's profile Send private message
skkp2006

New User


Joined: 14 Jul 2006
Posts: 93
Location: Chennai,India

PostPosted: Mon May 21, 2007 9:34 am
Reply with quote

Try this out......

Select * From Table A Where
(N-1) = (Select Count(Distinct(B.Empid)) From Table B Where
B.Empid > A.Empid)

Regards
Syam
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 Load new table with Old unload - DB2 DB2 6
No new posts Pulling a fixed number of records fro... DB2 2
No new posts Multiple table unload using INZUTILB DB2 2
No new posts Check data with Exception Table DB2 0
No new posts Dynamically pass table name to a sele... DB2 2
Search our Forums:

Back to Top