sql - Order by last digit in a number column in oracle 11 -


i have number column in table have order records basing on last digit of column value.

for ex, want records ends '2' , created 'john' first , remaining below
## number ## ## createdby ##

 3234445452         john       3432454542         john   3432454572         alex   1234567890         john   3432432441         john 

thanks help...

if want order last digit can use mod(value, 10). if want have ending 2 first (which suppose isn't stranger rest of requirement) can use case statement within order clause:

with t42 as(   select 3234445452 value dual   union select 3432454542 dual   union select 1234567890 dual   union select 3432432441 dual ) select value t42 order case when mod(value, 10) = 2 0 else 1 end, mod(value, 10), value;       value ---------- 3234445452  3432454542  1234567890  3432432441  

so puts ending 2 first, remainder ordered last digit, end 'buckets' 2, 0, 1, 3, 4, 5, ..., first 2 arguments of order by. third argument orders values numerically within each 'bucket', puts 3234445452 before 3432454542.


you can put other fields in order by, sure. need have case string too; might overkill:

with t42 as(   select 3234445452 num, 'john' createdby dual   union select 3432454542, 'john' dual   union select 3432454542, 'alex' dual   union select 1234567890, 'john' dual   union select 3432432441, 'john' dual ) select num, createdby t42 order case when mod(num, 10) = 2 0 else 1 end,   case when createdby = 'john' 0 else 1 end,   mod(num, 10), num, createdby;         num createdby ---------- --------- 3234445452 john       3432454542 john       3432454542 alex       1234567890 john       3432432441 john       

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -