Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist 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 HEX value search in a DB2 query maxsubrat DB2 2 Wed Oct 04, 2017 3:04 pm
No new posts Create procedure issues -628 when add... chandraBE DB2 1 Mon Sep 18, 2017 12:16 pm
No new posts Can we limit length in concatenation ... balaji81_k DB2 7 Tue Aug 22, 2017 2:50 am
No new posts Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am
No new posts JCL to Set Return code based on DB2 S... vinu78 DB2 17 Mon Mar 13, 2017 9:47 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us