Sql oralce中降序排列字符串和数字组合

Sql oralce中降序排列字符串和数字组合,sql,regex,oracle,select,sql-order-by,Sql,Regex,Oracle,Select,Sql Order By,我试图按降序排列字符串和数字的组合 输入: P9S1 P7S1 P13S1 P12S2 P10S1 预期产出: P13S1 P12S2 P10S1 P9S1 P7S1 这是我试过的 示例代码: with inputs (firmware) as ( select 'P9S1' from dual union all select 'P7S1' from dual union all select 'P13S1' from du

我试图按降序排列字符串和数字的组合

输入:

P9S1
P7S1
P13S1
P12S2
P10S1
预期产出:

P13S1
P12S2
P10S1
P9S1
P7S1
这是我试过的

示例代码:

  with
      inputs (firmware) as (
        select 'P9S1'  from dual union all
        select 'P7S1' from dual union all
        select 'P13S1' from dual union all
        select 'P12S2' from dual union all
        select 'P10S1'      from dual
      )
    select firmware
    from   inputs
    order by 
    regexp_replace(firmware, '\d+\.\d+') desc ;
但这并不像预期的那样有效。任何帮助都将不胜感激


谢谢

您实际上没有解释字符串如何变成数字

这将适用于您的数据集:

order by to_number(regexp_replace(firmware, '\D', '')) desc 
其思想是从字符串中删除所有非数字字符,将结果字符串转换为数字,并将其用于排序

with inputs (firmware) as (
    select 'P9S1' from dual union all
    select 'P7S1' from dual union all
    select 'P13S1' from dual union all
    select 'P12S2' from dual union all
    select 'P10S1' from dual
)
select firmware
from   inputs
order by to_number(regexp_replace(firmware, '\D', '')) desc ;
|固件| | :------- | |P13S1| |P12S2| |P10S1| |P9S1| |P7S1|
字符串始终保持“P”和“S”,因此不重要。这很好,正是我想要的。非常感谢您的快速回复。输入是否总是由“字母串”(至少一个字母),然后是“数字串”,然后(可选)更多交替的“字母串”和“数字串”组成?那么-您需要如何订购?第一部分总是一个字母
P
,还是其他字母?然后,似乎必须按第一组数字(视为整数)降序。如果还有关系,那又怎样?不要只展示一个小例子,期望它能说明数据中可能出现的所有特殊情况。 | FIRMWARE | | :------- | | P13S1 | | P12S2 | | P10S1 | | P9S1 | | P7S1 |