根据需要对字母数字字符进行排序

时间:2018-04-24 06:18:03

标签: database oracle

我将数据库中的数据设为[AD AC AB AA H7 H2 H4 H3 H8 H6 H5 H9 H1 HL HJ HH HG HF 1S 1B 1G 1A]我需要将其显示为[AD AC AB AA HL HJ HH HG HF H9 H8 H6 H7 H5 H4 H3 H2 H1 1S 1G 1B 1A]。你能帮我吗?

直到现在我已经尝试过这个

select distinct Col1
from    Table1
order by regexp_substr(Col1  , '^[[:alpha:]]') , 
regexp_substr(Col1  , '[[:alpha:]]', 1,2) desc,
regexp_substr(Col1  , '^\d*'),
regexp_substr(regexp_substr(Col1  , '_[[:alpha:]]*') ,'[[:alpha:]]+') desc;

提前致谢

2 个答案:

答案 0 :(得分:2)

试试这个

select distinct col1  
from table_name
order by 
regexp_substr(Col1 , '^[[:alpha:]]') ,
SUBSTR(Col1, 2, 1) desc

答案 1 :(得分:0)

您需要提供更多信息,或者针对其他案例更多地描述您的要求。

但是,根据您已经发布的数据,这是一个可以执行此操作的查询。

WITH mydat
     AS (SELECT 'AD' AS Col1 FROM DUAL
         UNION
         SELECT 'AC' AS Col1 FROM DUAL
         UNION
         SELECT 'AB' AS Col1 FROM DUAL
         UNION
         SELECT 'AA' AS Col1 FROM DUAL
         UNION
         SELECT 'H7' AS Col1 FROM DUAL
         UNION
         SELECT 'H2' AS Col1 FROM DUAL
         UNION
         SELECT 'H4' AS Col1 FROM DUAL
         UNION
         SELECT 'H3' AS Col1 FROM DUAL
         UNION
         SELECT 'H8' AS Col1 FROM DUAL
         UNION
         SELECT 'H6' AS Col1 FROM DUAL
         UNION
         SELECT 'H5' AS Col1 FROM DUAL
         UNION
         SELECT 'H9' AS Col1 FROM DUAL
         UNION
         SELECT 'H1' AS Col1 FROM DUAL
         UNION
         SELECT 'HL' AS Col1 FROM DUAL
         UNION
         SELECT 'HJ' AS Col1 FROM DUAL
         UNION
         SELECT 'HH' AS Col1 FROM DUAL
         UNION
         SELECT 'HG' AS Col1 FROM DUAL
         UNION
         SELECT 'HF' AS Col1 FROM DUAL
         UNION
         SELECT '1S' AS Col1 FROM DUAL
         UNION
         SELECT '1B' AS Col1 FROM DUAL
         UNION
         SELECT '1G' AS Col1 FROM DUAL
         UNION
         SELECT '1A' AS Col1 FROM DUAL)
  SELECT Col1,
         REGEXP_SUBSTR (Col1, '^[[:alpha:]]') first_char_else_null,
         REGEXP_SUBSTR (Col1, '^[[:digit:]]') first_num_else_null,
         SUBSTR (Col1, 2, 1) second_char,
         ASCII (SUBSTR (Col1, 2, 1)) ascii_second_char
    FROM mydat
ORDER BY REGEXP_SUBSTR (Col1, '^[[:alpha:]]') NULLS LAST,
         REGEXP_SUBSTR (Col1, '^[[:digit:]]') NULLS FIRST,
         ASCII (SUBSTR (Col1, 2, 1)) DESC;