如果您想按最后一位数字排序,您可以使用mod(value, 10)
. 如果你真的想要那些以2
first 结尾的(我想这并不比其余的要求更奇怪),那么你可以case
在 order by 子句中使用一个语句:
with t42 as(
select 3234445452 as value from dual
union all select 3432454542 from dual
union all select 1234567890 from dual
union all select 3432432441 from dual
)
select value
from t42
order by case when mod(value, 10) = 2 then 0 else 1 end, mod(value, 10), value;
VALUE
----------
3234445452
3432454542
1234567890
3432432441
所以这把那些以2
第一个结尾,然后是最后一个数字排序的余数,所以结尾的“桶”将是2, 0, 1, 3, 4, 5, ...
,从 的前两个参数开始order by
。然后第三个参数对每个“桶”中的值进行数字排序,放在3234445452
前面3432454542
。
当然,您可以将其他字段放在 中order by
。你也只需要一个case
字符串;这可能是矫枉过正:
with t42 as(
select 3234445452 as num, 'John' as createdby from dual
union all select 3432454542, 'John' from dual
union all select 3432454542, 'Alex' from dual
union all select 1234567890, 'John' from dual
union all select 3432432441, 'John' from dual
)
select num, createdby
from t42
order by case when mod(num, 10) = 2 then 0 else 1 end,
case when createdby = 'John' then 0 else 1 end,
mod(num, 10), num, createdby;
NUM CREATEDBY
---------- ---------
3234445452 John
3432454542 John
3432454542 Alex
1234567890 John
3432432441 John