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
 

 

sql query, second or third top employee..

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: sql query, second or third top employee..
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    Post subject: reply
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: 1452
Location: Chicago, IL

PostPosted: Mon Jan 02, 2006 10:03 am    Post subject: Re: sql query, second or third top employee..
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    Post subject:
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    Post subject: Re: sql query, second or third top employee..
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    Post subject: sql query, second or third top employee
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    Post subject:
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    Post subject:
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    Post subject:
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    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 2 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