Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ORDER BY DECODE(BLAH, [COLUMN NUMBER]) on a single column query. How does it work?

Hi I need help to understand the decode part of a query that goes something like the following.

SELECT ax.animal_code    
FROM  raw_animal_xref ax,
      animal_xref_type axt
WHERE ax.animal_mnemonic = l_animal_mnemonic -- Example 'COUGAR'
AND   ax.animal_code_type = axt.animal_code_type
ORDER BY DECODE (animal_type, 
                l_type_to_be_matched, -1, -- Example 'CATS'
                l_current_type, 0, -- Example 'BIG CATS'
                nvl(axt.type_search_priority, 100)) ASC; -- EXAMPLE 'Big Cats' Priority is 1

Since this query returns only 1 query, I'm a little stumped on how the ORDER BY works with the different non-existing column numbers supplied by DECODE. The query works as a cursor to find a unique code for the animal in question given a animal mnemonic the current animal type and the type to be matched with.

I'm thinking that DECODE returns the different column numbers to ORDER BY with and I tried experimenting with a different simple single column selects on some other tables with ORDER by '-1', '0' and '100' and the ORDER by seems to fail for 0 and 100. Why does it work with -1 or any of the other numbers?

Hope someone can explain this to me. Thanks!

like image 401
Azeworai Avatar asked Feb 19 '10 00:02

Azeworai


People also ask

How does decode work in SQL?

DECODE compares the expression to each search value one by one. If expression is equal to a search, then the corresponding result is returned by the Oracle Database. If a match is not found, then default is returned. If default is omitted, then Oracle returns null.

What is decode order?

order by decode(ename ,'e',1,'d',2,3) ; the ename column is not used at all for sorting ... it is just a parameter of the decode function the ordering is done by the function result of the decode function.

How does decode work in Oracle SQL?

The DECODE function returns a value that is the same datatype as the first result in the list. If the first result is NULL, then the return value is converted to VARCHAR2. If the first result has a datatype of CHAR, then the return value is converted to VARCHAR2. If no matches are found, the default value is returned.

How do you decode a value in SQL?

The basic syntax for writing DECODE function in SQL is as follows: DECODE (expression , search_1, result_1[, search_2, result_2], ...,[,search_n,result_n] [, default]); The parameters used in the above mentioned syntax are: expression: expression argument is the value which is to be searched and compared with.


2 Answers

The ORDER BY can use one of three expressions. Firstly an alias of the select list, secondly the number of a column in the select list or thirdly an SQL expression which may use zero or more columns from the source tables.

So when you use ORDER BY SUBSTR(col,2,10) you order by a 10 character substring of the column value starting from the second character.

Similarly when use

ORDER BY decode(col,'DOG',1,'CAT',2,'EEL', 3, 5)

you translate DOG into value 1, CAT into value 2, EEL into value 3 and others into value 5. Then order by the resulting numeric value (ie DOG first, then CAT, then EEL, finally anything else).

You can achieve the same ordering using

ORDER BY decode(col,'DOG','A','CAT','B','EEL', 'C', 'D')
like image 91
Gary Myers Avatar answered Sep 21 '22 09:09

Gary Myers


It creates the set that will be used for ordering.

If animal_type = l_type_to_be_matched, use a -1 as the sort value for that row
else if animal_type = l_current_type, use 0 as the sort value of that row
else if axt.type_search_priority is null then use 100 as the sort value of that row
else use axt.type_search_priority as the sort value for that row.

It gives a kind of conditional sort cirteria. Often used to ensure that certain items are always at the top or bottom of a sorted set.

like image 33
Doug Porter Avatar answered Sep 22 '22 09:09

Doug Porter