View previous topic :: View next topic |
Author |
Message |
mainframemouli Beginner
Joined: 23 Feb 2005 Posts: 87 Topics: 33
|
Posted: Fri Feb 25, 2005 5:06 am Post subject: Doubt in a Query for nth highest value |
|
|
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 |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
|
Back to top |
|
 |
mainframemouli Beginner
Joined: 23 Feb 2005 Posts: 87 Topics: 33
|
Posted: Mon Feb 28, 2005 12:11 am Post subject: |
|
|
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 |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Mon Feb 28, 2005 8:35 am Post subject: |
|
|
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 |
|
 |
|
|