Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom order in Oracle PL/SQL

I have an oracle query in which and i want the result to be in custom order 'SENIOR DIRECTOR', 'DIRECTOR', 'MANAGER', 'EMPLOYEE' which is from the field GRADE_DESCRIPTON. I am using the below query.

However I am not getting the desired result The order of the result im getting is 'SENIOR DIRECTOR','MANAGER', DIRECTOR,'EMPLOYEE'

SELECT DISTINCT GRADE_DESCRIPTION
      , HIRING_FORECATS.*  
FROM GRADE_MASTER left join     HIRING_FORECATS 
   ON (HIRING_FORECATS.GRADE = GRADE_MASTER.GRADE_DESCRIPTION 
        and HIRING_FORECATS.LOCATION = 'HO' )   
 order by decode    
 (GRADE_MASTER.GRADE_DESCRIPTION, 'SENIOR DIRECTOR', 'DIRECTOR', 'MANAGER', 'EMPLOYEE')

Any Suggestions??

like image 438
Andromeda Avatar asked May 11 '10 09:05

Andromeda


People also ask

How do I create a custom order in SQL?

By default SQL ORDER BY sort, the column in ascending order but when the descending order is needed ORDER BY DESC can be used. In case when we need a custom sort then we need to use a CASE statement where we have to mention the priorities to get the column sorted.

How do you sort in PL SQL?

When sorting your result set in descending order, you use the DESC attribute in your ORDER BY clause as follows: SELECT supplier_city FROM suppliers WHERE supplier_name = 'Microsoft' ORDER BY supplier_city DESC; This Oracle ORDER BY example would return all records sorted by the supplier_city field in descending order.

What is ORDER BY in Oracle SQL?

An ORDER BY clause allows you to specify the order in which rows appear in the result set.


1 Answers

ORDER BY DECODE(
         GRADE_MASTER.GRADE_DESCRIPTION,
         'SENIOR DIRECTOR', 1,
         'DIRECTOR', 2,
         'MANAGER', 3,
         'EMPLOYEE', 4,
         5)
like image 92
Quassnoi Avatar answered Sep 24 '22 15:09

Quassnoi