View previous topic :: View next topic
|
Author |
Message |
Ananya
New User
Joined: 26 Feb 2004 Posts: 19 Location: Chennai
|
|
|
|
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 |
|
|
Ananya
New User
Joined: 26 Feb 2004 Posts: 19 Location: Chennai
|
|
|
|
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.
Cheers
-Ananya |
|
Back to top |
|
|
harikrishnanrajeev
EXPERT
Joined: 28 Jan 2004 Posts: 37 Location: Trivandrum
|
|
|
|
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 |
|
|
harikrishnanrajeev
EXPERT
Joined: 28 Jan 2004 Posts: 37 Location: Trivandrum
|
|
|
|
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 |
|
|
|