table of Contents
When doing report display, you will encounter such a requirement to sort and display according to the order specified by a certain field, such as sorting according to the area segment from large to small. If this requirement is to directly use order by for sorting, the string is Sorting in lexicographical order is not the order we want. Oracle provides two ways to achieve this requirement:
Prepare test data first
drop table BR_DICT;
create table br_dict(
xl varchar2(32) not null,
mjd varchar2(32),
zb number(4,2)
);
insert into br_dict(xl, mjd, zb) values
('系列一', '≤60㎡', 2.05);
insert into br_dict(xl, mjd, zb) values
('系列一', '60-90㎡', 7.04);
insert into br_dict(xl, mjd, zb) values
('系列一', '90-120㎡', 36.92);
insert into br_dict(xl, mjd, zb) values
('系列一', '120-144㎡', 39.82);
insert into br_dict(xl, mjd, zb) values
('系列一', '144-180㎡', 9.41);
insert into br_dict(xl, mjd, zb) values
('系列一', '180-220㎡', 2.6);
insert into br_dict(xl, mjd, zb) values
('系列一', '>220㎡', 2.15);
insert into br_dict(xl, mjd, zb) values
('列一', '>220㎡', 2.15);
insert into br_dict(xl, mjd, zb) values
('一', '180-220㎡', 2.6);
insert into br_dict(xl, mjd, zb) values
('二', '144-180㎡', 9.41);
Solution one, realize the specified order sorting through order by instr
select *
from br_dict t
order by instr('≤60㎡,60-90㎡,90-120㎡,120-144㎡,144-180㎡,180-220㎡,>220㎡',mjd)
;
result:
系列一 ≤60㎡ 2.05
系列一 60-90㎡ 7.04
系列一 90-120㎡ 36.92
系列一 120-144㎡ 39.82
二 144-180㎡ 9.41
系列一 144-180㎡ 9.41
一 180-220㎡ 2.6
系列一 180-220㎡ 2.6
系列一 >220㎡ 2.15
列一 >220㎡ 2.15
Note: When using order by instr, the field name is placed at the back
Option 2: Through order by decode
select *
from br_dict t
order by decode
(mjd,
'≤60㎡', 1,
'60-90㎡', 2,
'90-120㎡', 3,
'120-144㎡', 4,
'144-180㎡', 5,
'180-220㎡', 6,
'>220㎡', 7)
;
result:
系列一 ≤60㎡ 2.05
系列一 60-90㎡ 7.04
系列一 90-120㎡ 36.92
系列一 120-144㎡ 39.82
二 144-180㎡ 9.41
系列一 144-180㎡ 9.41
一 180-220㎡ 2.6
系列一 180-220㎡ 2.6
系列一 >220㎡ 2.15
列一 >220㎡ 2.15
Note: When using order by instr, the field name is placed in front
Supplement: Sort by pinyin, gestures, and radicals
Prepare test data
drop table order_tb;
CREATE TABLE order_tb(
py VARCHAR2(32) NOT NULL,
bh VARCHAR2(32),
bs VARCHAR2(32)
);
insert into order_tb(py, bh, bs) values
('as', '一', '仁');
insert into order_tb(py, bh, bs) values
('ab', '二', '们');
insert into order_tb(py, bh, bs) values
('ab', '二', '们');
insert into order_tb(py, bh, bs) values
('rw', '三', '情');
insert into order_tb(py, bh, bs) values
('rw', '思', '思');
insert into order_tb(py, bh, bs) values
('bd', '四', '情');
insert into order_tb(py, bh, bs) values
('cd', '五', '据');
insert into order_tb(py, bh, bs) values
('c', '六', '次');
Sort using pinyin
The default raw order, you can add the desc keyword to set descending order
select *
from order_tb t
order by nlssort(py,'NLS_SORT=SCHINESE_PINYIN_M') --desc
;
result:
ab 二 们
ab 二 们
as 一 仁
as 一 仁
bc 三 怡
bc 三 怡
bd 四 情
bd 四 情
c 六 次
c 六 次
cd 五 据
cd 五 据
rw 思 思
rw 三 情
Sort using radicals
The default raw order, you can add the desc keyword to set descending order
select *
from order_tb t
order by nlssort(bs,'NLS_SORT=SCHINESE_RADICAL_M') --desc
;
result:
as 一 仁
as 一 仁
ab 二 们
ab 二 们
bc 三 怡
bc 三 怡
rw 思 思
rw 三 情
bd 四 情
bd 四 情
cd 五 据
cd 五 据
c 六 次
c 六 次
Sort by number of strokes
The default raw order, you can add the desc keyword to set descending order
select *
from order_tb t
order by nlssort(bh,'NLS_SORT=SCHINESE_STROKE_M') --desc
;
result:
as 一 仁
as 一 仁
ab 二 们
ab 二 们
bc 三 怡
bc 三 怡
rw 三 情
cd 五 据
cd 五 据
c 六 次
c 六 次
bd 四 情
bd 四 情
rw 思 思