关于一个ORACLE 排序的问题, 急!!!! 刚接手这块不久, 今天遇到了个问题, 以前开发的人直接把一个信息全部保存在一个字段里,导致现在用户需要这个字段进行排序,我很难实现, 坐等大神 with data as ( select 'CCC[2007]111号' as book_no from dual union all select 'CCC[2007]112号' as book_no from dual union all select 'CCC[2007]22号' as book_no from dual union all select 'CCC[2007]56号' as book_no from dual union all select 'AAAAAAA[2007]121号' as book_no from dual union all select 'AAAAAAA[2007]122号' as book_no from dual union all select 'AAAAAAA[2007]2号' as book_no from dual union all select 'AAAAAAA[2008]33号' as book_no from dual union all select 'BBBB[2007]111号' as book_no from dual union all select 'BBBB[2007]112号' as book_no from dual union all select 'BBBB[2008]3号' as book_no from dual union all select 'BBBB[2008]56号' as book_no from dual ) 就像这个。 把相应的字符串 CCC 或者BBBB 先放一起,然后根据中间的年份进行排序,然后再根据后面的数字进行排序,我这里有一个 select book_no from data order by substr (book_no, 1, instr (book_no, ']')), to_number ( replace (regexp_substr (book_no, '\]([[:digit:]]+)'), ']', ''))
select *
from data
order by regexp_substr(book_no,'[A-Z]+'),regexp_substr(book_no,'[0-9]+')
------解决方案--------------------
SQL code
select *
from data
order by regexp_substr(book_no,'[A-Z]+'),regexp_substr(book_no,'[0-9]+')
,to_number(substr(book_no,instr(book_no,']')+1,instr(book_no,'号')-instr(book_no,']')-1))
------解决方案--------------------
with t as (
select 'CCC[2007]111号' as book_no from dual union all
select 'CCC[2007]112号' as book_no from dual union all
select 'CCC[2007]22号' as book_no from dual union all
select 'CCC[2007]56号' as book_no from dual union all
select 'AAAAAAA[2007]121号' as book_no from dual union all
select 'AAAAAAA[2007]122号' as book_no from dual union all
select 'AAAAAAA[2007]2号' as book_no from dual union all
select 'AAAAAAA[2008]33号' as book_no from dual union all
select 'BBBB[2007]111号' as book_no from dual union all
select 'BBBB[2007]112号' as book_no from dual union all
select 'BBBB[2008]3号' as book_no from dual union all
select 'BBBB[2008]56号' as book_no from dual
)
select book_no, substr(book_no,1,instr(book_no,'[')-1) head,regexp_substr(book_no,'[[:digit:]]+',1,1) yy,
regexp_substr(book_no,'[[:digit:]]+',1,2) ed from t
order by head asc,yy desc,ed asc