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
 

 

N-nth Maximum Salary SQL Query

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
Ananya

New User


Joined: 26 Feb 2004
Posts: 19
Location: Chennai

PostPosted: Wed Mar 10, 2004 12:10 pm    Post subject: N-nth Maximum Salary SQL Query
Reply with quote

Dear All,

I was just searching for a QUERY which gives me the Nth Max sal of an employee. Here goes the SQL stmt....

Quote:
select max(sal)
from emp
where level = 8
connect by prior sal > sal
start with sal = (select max(sal) from emp)


Now my question is, can anyone explain me how this query WORKS ???
or atleast tell me what does the LEVEL cmd, CONNECT BY PRIOR and START WITH commands do?

Views are appreciated.

Thanks in advance.

Cheers
-Ananya 8)
Back to top
View user's profile Send private message

Ananya

New User


Joined: 26 Feb 2004
Posts: 19
Location: Chennai

PostPosted: Wed Mar 10, 2004 12:14 pm    Post subject: Nth Max Sal
Reply with quote

Opps ,

Replace the above Level value with &n instead and try working, it works real good. Only thing is during the execution time supply the value for 'n' and you will get the Nth Max Sal. but i'm not sure how this query works.

So pls, help me to understand this or do you guys have another solution for finding Nth max sal, if yes... kindly post it with explanation.

Thanks again. icon_rolleyes.gif

Cheers
-Ananya
Back to top
View user's profile Send private message
harikrishnanrajeev

EXPERT


Joined: 28 Jan 2004
Posts: 37
Location: Trivandrum

PostPosted: Sat Apr 03, 2004 9:52 pm    Post subject:
Reply with quote

Hello Ananya,

Does this Connect by prior work in DB2. I know that it works in Oracle.
Pls explain. If it does not work, did u get any other method to solve the problem of finding the nth max salary.

regards

Hari.
Back to top
View user's profile Send private message
harikrishnanrajeev

EXPERT


Joined: 28 Jan 2004
Posts: 37
Location: Trivandrum

PostPosted: Wed Apr 14, 2004 11:03 pm    Post subject:
Reply with quote

Hello Ananya,

I tried this query to find the nth max sal . It worked fine for me.

Quote:


select sal from emp A where n =

( select count(*) from emp B where A.sal < B.sal )



Here replace n with the required number.

Regards,

Hari.
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 0 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