Oracle SQL - 自定义排序
Oracle SQL - Custom Sort
我有一个场景,我有以下数据:
Table:地点
ID TYPE
------------------
1000 STORE
11001 STORE
20000 STORE
1181 WAREHOUSE
12002 STORE
我想以一种方式排序,所有以“0000”结尾的 ID 应该首先排序,然后是类型 'Warehouse',然后是商店的其余部分。
期望的输出应该像
ID TYPE
------------------
10000 STORE
20000 STORE
1181 WAREHOUSE
11001 STORE
12002 STORE
如何进行自定义排序?
您可以在排序中使用 case
表达式:
order by (case when id like '%0000' then 1
when type = 'WAREHOUSE' then 2
else 3
end), id
这也使用id
在三组内排序。
注意:如果id
是数字而不是字符串,我建议:
order by (case when mod(id, 10000) = 0 then 1
when type = 'WAREHOUSE' then 2
else 3
end), id
[LF 编辑]
这是您的 ORDER BY returns,这不是 OP 想要的:
SQL> with locations (id, type) as
2 (select 1000 , 'STORE' from dual union all
3 select 11001, 'STORE' from dual union all
4 select 20000, 'STORE' from dual union all
5 select 1181 , 'WAREHOUSE' from dual union all
6 select 12002, 'STORE' from dual
7 )
8 select id, type
9 from locations
10 order by (case when id like '%0000' then 1
11 when type = 'WAREHOUSE' then 2
12 else 3
13 end), id;
ID TYPE
---------- ---------
20000 STORE
1181 WAREHOUSE
1000 STORE
11001 STORE
12002 STORE
SQL>
Gordon 的评论:如果第 1000 行是 10000,以上应该有效。
我是这样理解问题的;样本数据直到第 7 行;查询从第 8 行开始。
SQL> with locations (id, type) as
2 (select 1000 , 'STORE' from dual union all
3 select 11001, 'STORE' from dual union all
4 select 20000, 'STORE' from dual union all
5 select 1181 , 'WAREHOUSE' from dual union all
6 select 12002, 'STORE' from dual
7 )
8 select id, type
9 from locations
10 order by case when substr(to_char(id), -3) = '000' then 1 end,
11 case when type = 'WAREHOUSE' then 2 end,
12 type;
ID TYPE
---------- ---------
1000 STORE
20000 STORE
1181 WAREHOUSE
12002 STORE
11001 STORE
SQL>
我有一个场景,我有以下数据:
Table:地点
ID TYPE
------------------
1000 STORE
11001 STORE
20000 STORE
1181 WAREHOUSE
12002 STORE
我想以一种方式排序,所有以“0000”结尾的 ID 应该首先排序,然后是类型 'Warehouse',然后是商店的其余部分。
期望的输出应该像
ID TYPE
------------------
10000 STORE
20000 STORE
1181 WAREHOUSE
11001 STORE
12002 STORE
如何进行自定义排序?
您可以在排序中使用 case
表达式:
order by (case when id like '%0000' then 1
when type = 'WAREHOUSE' then 2
else 3
end), id
这也使用id
在三组内排序。
注意:如果id
是数字而不是字符串,我建议:
order by (case when mod(id, 10000) = 0 then 1
when type = 'WAREHOUSE' then 2
else 3
end), id
[LF 编辑]
这是您的 ORDER BY returns,这不是 OP 想要的:
SQL> with locations (id, type) as
2 (select 1000 , 'STORE' from dual union all
3 select 11001, 'STORE' from dual union all
4 select 20000, 'STORE' from dual union all
5 select 1181 , 'WAREHOUSE' from dual union all
6 select 12002, 'STORE' from dual
7 )
8 select id, type
9 from locations
10 order by (case when id like '%0000' then 1
11 when type = 'WAREHOUSE' then 2
12 else 3
13 end), id;
ID TYPE
---------- ---------
20000 STORE
1181 WAREHOUSE
1000 STORE
11001 STORE
12002 STORE
SQL>
Gordon 的评论:如果第 1000 行是 10000,以上应该有效。
我是这样理解问题的;样本数据直到第 7 行;查询从第 8 行开始。
SQL> with locations (id, type) as
2 (select 1000 , 'STORE' from dual union all
3 select 11001, 'STORE' from dual union all
4 select 20000, 'STORE' from dual union all
5 select 1181 , 'WAREHOUSE' from dual union all
6 select 12002, 'STORE' from dual
7 )
8 select id, type
9 from locations
10 order by case when substr(to_char(id), -3) = '000' then 1 end,
11 case when type = 'WAREHOUSE' then 2 end,
12 type;
ID TYPE
---------- ---------
1000 STORE
20000 STORE
1181 WAREHOUSE
12002 STORE
11001 STORE
SQL>