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 

Doubt in a Query for nth highest value

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


Joined: 23 Feb 2005
Posts: 87
Topics: 33

PostPosted: Fri Feb 25, 2005 5:06 am    Post subject: Doubt in a Query for nth highest value Reply with quote

I searched the site but i got a Query like the one i have given below to get the Top specified values but if the table contains the duplicates


consider the record is like this

RDS_SKU_NBR
1000
2000
2000
3000
4000

I will order it in desecding order and if give this query then third highest
should be 2000 but its not working but if i give "where 4 ="

then its giving the result 2000.



SELECT A.RDS_SKU_NBR FROM DEV.WMS_LOC_INV A WHERE 3 = (SELECT COUNT(*) FROM

DEV.WMS_LOC_INV B WHERE B.RDS_SKU_NBR >= A.RDS_SKU_NBR)



and also tell me the query for finding the top n highest sku number ??????
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: Fri Feb 25, 2005 8:27 am    Post subject: Reply with quote

mainframemouli,

Check this link which answers your queries

http://www.mvsforums.com/helpboards/viewtopic.php?t=23&highlight=salary

Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
mainframemouli
Beginner


Joined: 23 Feb 2005
Posts: 87
Topics: 33

PostPosted: Mon Feb 28, 2005 12:11 am    Post subject: Reply with quote

I checked the link the queries given there are like this


This query is for nth highest
SELECT * FROM EMP_TABLE
WHERE SAL >= (SELECT MAX(SAL) FROM EMP_TABLE
WHERE SAL < (SELECT MAX(SAL) FROM EMP_TABLE
WHERE SAL < (SELECT MAX(SAL)
FROM EMP_TABLE)))
ORDER BY SAL DESC
;


This query is for n highest
SELECT *
FROM
EMP_TABLE X
WHERE n > ( SELECT COUNT(*) FROM EMP_TABLE
WHERE SAL > X.SAL )
ORDER BY X.SAL DESC

my questions are

1.if i want the 10th maximum value , shall i have to give 10 subqueries?????

2.If i want the 16th max what i have to do?????

3. Is the Query for N highest value will work if duplicates are there????
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: Mon Feb 28, 2005 8:35 am    Post subject: Reply with quote

mainframemouli,
Quote:

if i want the 10th maximum value , shall i have to give 10 subqueries?????


NO. You can use the second query in your post. Replace N with 10.

Quote:

2.If i want the 16th max what i have to do?????


Same as above replace N with 16.

Quote:

3. Is the Query for N highest value will work if duplicates are there????


If you had spend some time reading the link, then you would have found this

That is a query to select 'Top n, with ties for the last place' and NOT 'First n'.

Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
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