Finding Nth Minimum of a Varchar value in Oracle

后端 未结 2 1854
天涯浪人
天涯浪人 2021-01-26 17:10

I have an oracle table with the columns Config_ID, Escalation_Level

In this table the Escalation_Level is a Varchar with the values \'L0\',\'L1\',\'

相关标签:
2条回答
  • 2021-01-26 17:47

    If you want to find the Nth value of anything then the analytic function NTH_VALUE() is a good place to start.

    Assuming you want this based on the numeric part only you have to replace everything that is not a number, for which you can use REGEXP_REPLACE()

    select regexp_replace(escalation_level, '[^[:digit:]]')
      from my_table
    

    To obtain the Nth value for a given CONFIG_ID it would be:

    select nth_value(escalation_level, n)
             over ( partition by config_id 
                        order by regexp_replace(escalation_level, '[^[:digit:]]') )
      from my_table
    

    where n is the index of the value you want to return.

    0 讨论(0)
  • 2021-01-26 17:47

    Just a hint, you should use

    RANK() OVER (PARTITION BY Config_ID ORDER BY Escalation_Level) 
    

    in this way you will be able to make Oracle to give you the Nth position of each Escalation_Level. When you should apply a filter.

    Anyway check the RANK and the DENSE_RANK analytic functions in the Oracle documentation

    0 讨论(0)
提交回复
热议问题