0

I have a table products and i have column standard_cost and I want to get the standard cost of the product which lies in 10th highest position. So far I tried

SELECT t1.* 
FROM (SELECT  STANDARD_COST 
      FROM OT.PRODUCTS 
      ORDER BY STANDARD_COST DESC) t1  
WHERE ROWNUM=1 
ORDER BY t1.STANDARD_COST ASC;

But it's giving me the same value when I first sorted descending of inner query and then ascending in ORACLE. What is the problem in my query?

APC
  • 144,005
  • 19
  • 170
  • 281
Ashwin Karki
  • 249
  • 4
  • 18

2 Answers2

1

There are any ways to find nth highest cost.

SELECT T1.STANDARD_COST
FROM (
    SELECT STANDARD_COST, DENSE_RANK() OVER (ORDER BY STANDARD_COST DESC) nth_highest_cost 
    FROM PRODUCTS
) T1
WHERE nth_highest_cost = 10;

You can look for other methods from following reference:

Dark Knight
  • 6,116
  • 1
  • 15
  • 37
0

You can use nth_value analytical function.

Select * from
(select t.*, DISTINCT NTH_VALUE(t.standard_cost,10)
       OVER (ORDER BY t.standard_cost desc
       RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
       AS TENTH_HIGHEST
FROM PRODUCTS t)
Where standard_cost = tenth_highest;

Cheers!!

Popeye
  • 35,427
  • 4
  • 10
  • 31