Special Oracle Order

oracleorder-by

I have a table (emp) with two columns in ORACLE:

COMPANY   OFFICE    EMP
------------------------
9999      00001     emp1
9999      00001     emp2
9999      00002     emp3
9999      00002     emp4
0001      99999     emp5
0001      00001     emp6
0001      00002     emp7
0002      00001     emp8
0002      99999     emp9
0003      99999     emp10
0003      00001     emp11
0003      00002     emp12

I want to get value in EMP column with following statement:

SELECT 
   EMP 
FROM 
   emp
ORDER BY COMPANY /*(But 9999 is first)*/, OFFICE /*(But 99999 is first).*/

I don't know how to do.

Best Answer

I guess you want the results in ascending order but with 9999 always first?

Select EMP
From Emp
Order By Case When Company = 9999 Then -1 else Company End,
         Case When Office = 9999 Then -1 else Office End;