Oracle SQL Order By Alphabetical THEN Numeric

时间:2017-07-31 09:52:51

标签: sql oracle

使用Oracle商业智能(OTBI),我可以使用ORDER BY查询先按字母顺序对列进行排序,然后使用Numerics?

E.g。

|---------------------|
|  Expenditure Type   |    
|---------------------|
|   111 Contractors   |       
|---------------------|
|   112 Payroll       |
|---------------------|
|   Annual Leave      |
|---------------------|
|   Christmas Leave   |
|---------------------|

我希望它能带回来:

|---------------------|
|  Expenditure Type   |    
|---------------------|
|    Annual Leave     |       
|---------------------|
|    Christmas Leave  |
|---------------------|
|    111 Contractors  |
|---------------------|
|    112 Payroll      |
|---------------------|

2 个答案:

答案 0 :(得分:0)

看看我的例子:

with t as (select 'Expenditure Type' as a from dual
union all
select '111 Contractors' from dual
union all
select '112 Payroll' from dual
union all
select 'Annual Leave' from dual
union all
select 'Christmas Leave' from dual)

select * 
from (
    select a, 
                 DECODE(replace(replace(translate(UPPER(a),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','##########################'),'#'),' '),NULL,'ALPHABET','NUMBER') as typ
    from t
)
order by typ

答案 1 :(得分:0)

您看到的行为差异可能是因为NLS_SORT参数设置不同。考虑:

SQL> select * from nls_session_parameters where parameter='NLS_SORT';

参数值

NLS_SORT BINARY

SQL> SELECT * FROM my_data order by title;

标题

321 ABC DEF

SQL> alter session set nls_sort=french;

会话改变了

SQL> SELECT * FROM my_data order by title;

标题

ABC 高清 321