MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how this query find 3rd maximum salary from table

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
rahul780
Beginner


Joined: 04 Jan 2008
Posts: 10
Topics: 9

PostPosted: Thu Jan 31, 2008 6:59 am    Post subject: how this query find 3rd maximum salary from table Reply with quote

hi all,

i have a SQL query which find 3rd maximum salary from table but i can't understand how this work takes place

Query
SELECT SALALARY FROM EMPINFO A
WHERE 3 = (SELECT COUNT (DISTINCT (SALARY) FROM EMPINFO
WHERE SALARY > = A.SALARY

THIS IS CORELATED QUERY

AND IN MY TABALE SALARY COLLOME CONSIT FOLLOWING SALARYS

SALARY
10000
10000
40000
30000
50000
60000
60000

This query give proper output that is 3rd maximum salary=40000
but how this query finds this

regards
rahul
Back to top
View user's profile Send private message
vkphani
Intermediate


Joined: 05 Sep 2003
Posts: 483
Topics: 48

PostPosted: Thu Jan 31, 2008 7:34 am    Post subject: Reply with quote

rahul,

That 3 is nothing but row id. Each value of the column salary is comapred against rest of the values in the same column.

First 10000 is compared with all the other values and the count will be 5 as there are 5 values greater than or equal to 10000 in that column.

Then 30000 is compared and the count will be 4.

Then 40000 is compared and the count will be 3.

Then 50000 is compared and the count will be 2.

Then 60000 is compared and the count will be 1.

So based on the row id 3, 40000 is given as result.
Back to top
View user's profile Send private message Send e-mail
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Thu Jan 31, 2008 1:32 pm    Post subject: Reply with quote

So I thought I might be able to use a query like this in the future. I've spent about 1 hours trying to make a simple query work with zero luck. Maybe someone can point out my error(s). I made a couple of small changes to my query like '(' , name the table. Also I tried COUNT(*).

Code:

SELECT b.CHK_amt FROM fdbmd.med_recon A
WHERE 3 = (SELECT COUNT( DISTINCT (B.CHK_AMT)
                       FROM fdbmd.med_recon b
                       WHERE b.chk_amt >=  A.chk_amt )

_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Thu Jan 31, 2008 1:47 pm    Post subject: Reply with quote

NASCAR9,

try this

Code:

SELECT A.CHK_amt FROM fdbmd.med_recon A
WHERE 3 = (SELECT COUNT(DISTINCT(B.CHK_AMT))
                       FROM fdbmd.med_recon b
                       WHERE b.chk_amt >=  A.chk_amt )


Hope this helps...
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Thu Jan 31, 2008 2:14 pm    Post subject: Reply with quote

Thank you kolusu! Your query worked fine.
_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
rahul780
Beginner


Joined: 04 Jan 2008
Posts: 10
Topics: 9

PostPosted: Thu Jan 31, 2008 11:28 pm    Post subject: Reply with quote

thank you vkphani and kolusu for giving help
the query given by kolusu wokes

thanks
rahul
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group