ORACLE中如何获取第10高的成本?

How to get the 10th highest cost in ORACLE?

我有一个 table 产品,我有 standard_cost 列,我想获得位于第 10 位的产品的标准成本。到目前为止我试过

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

但是当我首先对内部查询进行降序排序然后在 ORACLE 中进行升序排序时,它给了我相同的值。我的查询有什么问题?

有任何方法可以找到第 n 个最高成本。

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;

您可以参考以下方法寻找其他方法:

可以使用nth_value解析函数。

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;

干杯!!