Use of decode function in sql in oracle

if-then-else logic in DECODE

In logic programming, If-Then-Else is often used to make logical judgments. In the grammar of DECODE, it is actually such a logical processing process. Its syntax is as follows:
DECODE(value, if1, then1, if2,then2, if3,then3, . . . else )
Value represents any column of any type in a table or any result of a calculation. When each value is tested, if the value of value is if1, the result of the Decode function is then1; if value is equal to if2, the result of the Decode function is then2; and so on. In fact, multiple if/then pairs can be given. The Decode result returns else if the value result is not equal to any of the given pairs.
It should be noted that if, then and else can all be functions or calculation expressions.

Simple example of DECODE

There are many data dictionaries in the Oracle system that are designed with the idea of ​​decode, such as the V$SESSION data dictionary view that records session information. We learned from the "Oracle8i/9i Reference" data that when a user logs in successfully, there is a corresponding record of the user in V$SESSION, but the command operation performed by the user only records the command code in this view (0— No action, 2—Insert...), not a specific command keyword. Therefore, when we need to know the names of the current users and their operations, we need to use the following commands to get detailed results:
select sid,serial#,username,
DECODE(command,
0,’None’,
2,’Insert’,
3,’Select’,
6,’Update’,
7,’Delete’,
8,’Drop’,
‘Other’) command
from v$session where username is not null;
Example 2:
DECODE(VALUE,'0','1','2')
1 if value is 0, 2 otherwise.

DECODE realizes the transposition of the table

A table in a database is a two-dimensional . Generally, the number of columns in any database is limited, and the variation of rows is large. If the table is large, the number of rows may be tens of millions of rows. Different rows of the same column may have different values ​​and are not predefined.
Example: Example of housing provident fund statement replacement:
1. Each unit opens an account in the local handling bank. Opening an account is to register the basic information of the unit and employee information;
2. The accountants of each unit pay the housing provident fund of all employees of the unit to the handling bank every month. The system records the payment details of each employee and records the code of the handling bank on each record;
3. For each month, quarter, half year and year end, it is required to turn the handling line into a "column" to give detailed reports for each month:
Handling Bank: Chengdong District , Chengxi District
month:
2001.01 xxxx1.xx xxxxx2.xx
2001.02 xxxx3.xx xxxxx4.xx
。 。 。 。 。 。
The original data order is:
Chengxi District 2001.01 xxxxx1.xx
Chengdong District 2001.01 xxxxx2.xx
Chengxi District 2001.02 xxxxx3.xx
Chengdong District 2001.02 xxxxx4.xx
The structure of the pay_lst table for the housing provident fund system to record the monthly payment details of employees is:
bank_code varchar2(6)NOT NULL, -- bank code
acc_no varchar2(15) not null, -- unit code (unit account number)
emp_acc_no varchar2(20) not null, -- employee account number
tran_date date not null, -- payment date
tran_val Number(7,2) not null, -- payment amount
sys_date date default sysdate, --system date
oper_id varchar2(10) -- operator code
With such a table structure, it is generally easy to count the transaction rows as rows, but it is difficult to output the transaction rows in a format such as columns. If you use the DECODE function to deal with it, it becomes very simple:
We create a view to query the current pay_lst table. Change the handling bank code into some specific handling bank names:
CREATE OR REPLACE VIEW bank_date_lst AS
Select to_char(tran_date,’yyyy.mm’),
SUM( DECODE ( bank_code, '001', tran_val, 0 )) City West ,
SUM( DECODE ( bank_code,'002', tran_val,0 )) south of the city,
SUM( DECODE ( bank_code,'003', tran_val,0 )) Chengdong District
FROM pay_lst
GROUP BY to_char(tran_date,’yyyy.mm’);
After creating a view, you can directly query the view to display the results by column.

Guess you like

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