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

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:

ORDER BY COMPANY /*(But 9999 is first)*/, OFFICE /*(But 99999 is first).*/

I don't know how to do.

  • I don't understand the output you want from your query. Can you write down the output you're expecting and then we can help you with your query. – Trygve Laugstøl Nov 23 '12 at 23:51
  • Thanks dezso, my problem was solved, by Bary's way, thank you very much. – TOMMY Dec 8 '12 at 16:23

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;

If I understand you correctly, you want to sort based on company in descending order, and then by office also in descending order

SELECT emp FROM emp ORDER BY company DESC, office DESC;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.