Search code examples
sqloracle-databaseplsql

Order by with a particular value on TOP


I have a requirement to order a list of countries alphabetically but with a specific country on TOP. After that country it should be ordered alphabetically . Example

India
Afghanistan
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antigua and Barbuda
Argentina
Armenia
Aruba 

........... I tried the answer provided here Sorting certain values to the top but it was not working I am using PL/SQl dev tool. Thanx in Advance


Solution

  • Thanx all for the response. This way i tried.

     SELECT *
            FROM (SELECT L.LOOK_UP_CODE, TO_CHAR(L.CITY) LOOK_UP_DESC
                    FROM GHCM_IN_CITIES_MAPPING_DTLS L, GHCM_LOOK_UP_TBL A
                   WHERE L.ENABLED_FLAG = 'Y'
                     AND L.STATE = IN_STATE
                     AND A.LOOK_UP_TYPE = 'LOCATION_VALUE'
                  UNION
                  SELECT A.LOOK_UP_CODE LOOK_UP_CODE,
                         A.LOOK_UP_DESC LOOK_UP_DESC
                    FROM GHCM_LOOK_UP_TBL A
                   WHERE A.LOOK_UP_TYPE = 'LOCATION_VALUE')
           ORDER BY (CASE
                      WHEN LOOK_UP_DESC = 'Others' THEN
                       'ZZZ'
                      ELSE
                       LOOK_UP_DESC
                    END);
    

    And its working perfectly.