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??
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.
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.
An ORDER BY clause allows you to specify the order in which rows appear in the result set.
ORDER BY DECODE(
GRADE_MASTER.GRADE_DESCRIPTION,
'SENIOR DIRECTOR', 1,
'DIRECTOR', 2,
'MANAGER', 3,
'EMPLOYEE', 4,
5)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With