Use of decode function in Oracle

Meaning explanation: 
decode (condition, value 1, return value 1, value 2, return value 2, ... value n, return value n, default value)

The meaning of this function is as follows:
IF condition=value1 THEN
    RETURN(translation value1)
ELSIF condition=value2 THEN
    RETURN(translation value2)
    ......
ELSIF condition=valuen THEN RETURN(translation valuen
    )
ELSE
    RETURN (default)
END IF

decode (field or field operation, value 1, value 2, value 3)

       The result of this function running is that when the value of the field or the operation of the field is equal to the value 1, the function returns the value 2, otherwise it returns the value 3
 , of course, the value 1, the value 2, and the value 3 can also be an expression, this function makes some sql statement is much simpler

Usage: 

1. Compare the size
select  decode (sign(variable 1-variable 2), -1, variable 1, variable 2) from dual; -- take the smaller value
sign() function according to a certain value is 0, positive number If it is a negative number, it will return 0, 1, and -1 respectively.
For example, if
variable 1=10 and variable 2=20
, then sign (variable 1-variable 2) returns -1, and the decoded result of decode is "variable 1", and the smaller value is reached. the goal of.

 2. This function is used in SQL statements . The functions are described as follows:

  The Decode function is similar to a series of nested IF-THEN-ELSE statements. base_exp is compared with compare1, compare2, etc. in turn. If base_exp matches the ith compare item, return the ith corresponding value. If base_exp does not match any of the compare values, default is returned. Each compare value is evaluated in turn, and if a match is found, the remaining compare values ​​(if any) are not evaluated. A NULL base_exp is considered equivalent to a NULL compare value. If necessary, each compare value is converted to the same data type as the first compare value, which is also the type of the return value.

  The Decode function is very useful in actual development

Combined with the Lpad function, how to automatically add 1 to the value of the primary key and add 0 to the front
select  LPAD( decode( count(record number),0,1,max(to_number(record number)+1) ) , 14,'0' )  record number  from tetdmis

  eg:

 select decode(dir,1,0,1) from a1_interval

The value of dir is 1 to 0, and 0 to 1

 For example, I want to check the number of boys and girls in a certain class?

 Usually we write this:

select count(*) from table where gender = male;

select count(*) from table where gender = female;

If you want to display it together, you have to union it, it's too troublesome

用decode呢,只需要一句话

select sum(decode(性别,男,1,0)),sum(decode(性别,女,1,0)) from 表

eg:

select sum(decode(siteno,'LT',1,0)),sum(decode(siteno,'SZ',1,0)) from facd605;

select sum(case siteno when 'LT' then 1 else 0 end),sum(case siteno when 'SZ' then 1 else 0 end) from facd605;

vinson

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325495102&siteId=291194637