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

N-nth Maximum Salary SQL Query


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
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
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
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
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
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts DB2: Need helping below DB2 query DB2 13
No new posts DB2 SQL query to read and update data... DB2 12
No new posts Cobol-db2 : SQL query is running for ... DB2 4
No new posts Reading subsequent rows in a query. DB2 12
No new posts SYSIBM Tables Query DB2 8
Search our Forums:

Back to Top