#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Order By In Oracle With Priority


    I have a table (emp) with three 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. Please help me do this. Thank in advance!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2003
    Location
    Paris Uppland
    Posts
    2,385
    Rep Power
    391
    Code:
    select emp
      from emp
     order
        by case when company = 9999 then 0 else 1 end,
           company,
           case when office = 99999 then 0 else 1 end,
           office
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    It helps me. Thank in advance!

IMN logo majestic logo threadwatch logo seochat tools logo