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

sql query, second or third top employee..


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

New User


Joined: 26 Aug 2005
Posts: 33
Location: bangalore

PostPosted: Sun Jan 01, 2006 10:19 pm
Reply with quote

hi experts,

plz tell me the sql query to retrieve second or third top (based on salary)
employee in a org.

regards
mija
Back to top
View user's profile Send private message
aklima

New User


Joined: 25 Nov 2005
Posts: 18
Location: uae

PostPosted: Mon Jan 02, 2006 6:38 am
Reply with quote

hi,

select * from employee where salary =(select max(salary) from employee where salary < (select max(salary) from employee))

i hope this will work for the second max

bye
Back to top
View user's profile Send private message
priyesh.agrawal

Senior Member


Joined: 28 Mar 2005
Posts: 1448
Location: Chicago, IL

PostPosted: Mon Jan 02, 2006 10:03 am
Reply with quote

aklima,
Quote:
i hope this will work for the second max

Yeah, it would.

But this approach can be very tedious while making for big numbers like TOP 25th ....As it needs to cascade Subqueries inturn,

You can give a try to this query ....
Code:
SELECT EMP-ID, EMP-NAME, SALARY FROM <TABLE-NAME> A
WHERE N = (SLECT COUNT(*) FROM <TABLE-NAME> B WHERE B.SALARY<A.SALARY);


Regards,

Priyesh.
Back to top
View user's profile Send private message
aklima

New User


Joined: 25 Nov 2005
Posts: 18
Location: uae

PostPosted: Mon Jan 02, 2006 10:29 am
Reply with quote

sure priyesh i shall try out this
bye and thanx
Back to top
View user's profile Send private message
cheli

New User


Joined: 21 Jul 2005
Posts: 11

PostPosted: Mon Jan 02, 2006 11:17 am
Reply with quote

Hi...a small correction the query given by Priyesh...

Code:
SELECT EMP-ID, EMP-NAME, SALARY FROM <TABLE-NAME> A
WHERE N-1 = (SLECT COUNT(*) FROM <TABLE-NAME> B WHERE B.SALARY<A.SALARY);


[/code]
Here N gives the no:of Nth maximum as u required.

Bye..
Back to top
View user's profile Send private message
rajesh_1183

Active User


Joined: 24 Nov 2005
Posts: 121
Location: Tadepalligudem

PostPosted: Mon Jan 02, 2006 11:51 am
Reply with quote

Hi,

Can anybody explain the query step by step how it works with an example

Thanks,
Rajesh
Back to top
View user's profile Send private message
winchaat

New User


Joined: 23 Mar 2005
Posts: 14

PostPosted: Mon Jan 02, 2006 12:11 pm
Reply with quote

A few corrections in above replied answers:

For nth maximum salary query should be like this

SELECT salary FROM HUND7.EMPLOY A WHERE N=(SELECT COUNT (DISTINCT(SALARY)) FROM HUND7.EMPLOY B WHERE A.SALARY<=B.SALARY)


if we missed distinct keyword then duplication rows also counted and give
the result as some other value.


explanation
The above query takes one row from a table and compare with each row based on the salary value and get the count for that which should be equal to value u given.
Back to top
View user's profile Send private message
kumar_s

New User


Joined: 15 Mar 2006
Posts: 2

PostPosted: Wed Mar 15, 2006 4:35 pm
Reply with quote

May i know how to find the same, with making use of 'rownumber' funcition.
Back to top
View user's profile Send private message
kumar_s

New User


Joined: 15 Mar 2006
Posts: 2

PostPosted: Thu Mar 16, 2006 9:47 am
Reply with quote

Or may I know the Syntax to use the ROWNUMBER function.
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 RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
No new posts Issue with EXEC CICS QUERY SECURITY c... CICS 6
Search our Forums:

Back to Top