当前位置:网站首页>Oracle:decode function
Oracle:decode function
2022-06-12 04:19:00 【Hello】
One 、 sketch
DECODE The function is ORACLE PL/SQL One of the powerful functions of , At present, only ORACLE The company's SQL Provides this function , Of other database manufacturers SQL The implementation doesn't have this feature yet .DECODE What's the use of it ?
Let's start with an example , Suppose you want to increase the salary of the staff , The standard is : Wages in 8000 If it's less than yuan, add 20%; Wages in 8000 Yuan or above plus 15%, The usual way is , First, select the salary field value in the record select salary into var-salary from employee And then for variables var-salary use if-then-else or case when then else end And other flow control statements . If you use DECODE function , Then you can omit these flow control statements , adopt SQL Statement can be completed directly . as follows :
select
decode(sign(salary-8000),
-1,salary*1.2,
1,salary*1.15,
salary*1.15)
from employee
Isn't that neat ? DECODE The grammar of :DECODE(value,if1,then1,if2,then2,if3,then3,…,else), Said if value be equal to if1 when ,DECODE The result of the function returns then1,……, If it's not equal to any one if value , Then return to else. For the first time ,DECODE You can only do the equivalent test , But I just saw , Replace by some function or calculation value, Yes, it can make DECODE The function has a value greater than 、 Less than or equal to the function .
1、 Compare the size
select decode(sign( Variable 1- Variable 2),-1, Variable 1, Variable 2) from dual; -- Take the smaller value
sign() The function is... Based on a value 0、 Positive or negative , Return respectively 0、1、-1
for example :
Variable 1=10, Variable 2=20
be sign( Variable 1- Variable 2) return -1,decode The decoding result is “ Variable 1”, The purpose of taking a smaller value is achieved .
2、 surface 、 View structure transformation
There's a sales list sale, The structure of the table is :
month char(6) -- month
sell number(10,2) -- Monthly sales amount
The available data are :
200001 1000
200002 1100
200003 1200
200004 1300
200005 1400
200006 1500
200007 1600
200101 1100
200202 1200
200301 1300
You want to convert data into the following structure :
year char(4) -- year
month1 number(10,2) --1 Monthly sales amount
month2 number(10,2) --2 Monthly sales amount
month3 number(10,2) --3 Monthly sales amount
month4 number(10,2) --4 Monthly sales amount
month5 number(10,2) --5 Monthly sales amount
month6 number(10,2) --6 Monthly sales amount
month7 number(10,2) --7 Monthly sales amount
month8 number(10,2) --8 Monthly sales amount
month9 number(10,2) --9 Monthly sales amount
month10 number(10,2) --10 Monthly sales amount
month11 number(10,2) --11 Monthly sales amount
month12 number(10,2) --12 Monthly sales amount
Structural transformation SQL Statement for :
create or replace view
v_sale(year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)
as
select
substrb(month,1,4),
sum(decode(substrb(month,5,2),'01',sell,0)),
sum(decode(substrb(month,5,2),'02',sell,0)),
sum(decode(substrb(month,5,2),'03',sell,0)),
sum(decode(substrb(month,5,2),'04',sell,0)),
Add 1:
Student transcript student, Now use decode The function implements the following functions : achievement >85, Show excellence ;>70 Show good ;>60 pass ; Otherwise, I will fail .
hypothesis student The number of is id, The result is score, that :
select id,
decode(sign(score-85),
1,' good ',
0,' good ',
-1, decode(sign(score-70),
1,' good ',
0,' good ',
-1, decode(sign(score-60),
1,' pass ',
0,' pass ',
-1,' fail, ')))
from student;
Add 2:
Decode The syntax structure of the function is as follows :
decode (expression, search_1, result_1)
decode (expression, search_1, result_1, search_2, result_2)
decode (expression, search_1, result_1, search_2, result_2, ...., search_n, result_n)
decode (expression, search_1, result_1, default)
decode (expression, search_1, result_1, search_2, result_2, default)
decode (expression, search_1, result_1, search_2, result_2, ...., search_n, result_n, default)
decode Function to compare an expression with a search word , If the match returns a result ; If it doesn't match , return default value ; If not defined default value , Return null value .
边栏推荐
- Work report of epidemic data analysis platform [6] visual drawing
- WiFi module scheme of the wireless Internet of things, esp32-s3 chip technology, helps the equipment to be intelligent
- Eight fallacies of distributed computing
- spacy中en_core_web_sm安装问题
- Notes on relevant knowledge points such as original code / inverse code / complement code, size end, etc
- Absolute positioning three ways to center the box
- Summary of sequential, modulelist, and moduledict usage in pytorch
- Work report of epidemic data analysis platform [4] cross domain correlation
- Concept and introduction of microservice
- Plot visualization in R language: visualize the scatter diagram of the actual value and the predicted value of the regression model, analyze the prediction efficiency of the regression model, distingu
猜你喜欢

E-commerce middle office system architecture

疫情数据分析平台工作报告【7】阿里云相关

Recommended system cleaning tools, cocktail Download

Implementation of fitness club management system based on SSH

Construction case of Expressway Precast Beam Yard (with scheme text)

Legendary biological car-t has been approved by FDA, becoming the first domestic cell therapy product to successfully go to sea

疫情数据分析平台工作报告【1】数据采集
![[fpga+fft] design and implementation of FFT frequency meter based on FPGA](/img/f9/e985bd9aed332982d936b5d4e0d42b.png)
[fpga+fft] design and implementation of FFT frequency meter based on FPGA

Zabbix6.0新功能Geomap 地图标记 你会用吗?

智能面板WiFi聯動技術,ESP32無線芯片模組,物聯網WiFi通信應用
随机推荐
[fpga+fft] design and implementation of FFT frequency meter based on FPGA
【FPGA+GPS接收器】基于FPGA的双频GPS接收器详细设计介绍
如何制作数据集并基于yolov5训练成模型并部署
【C语言】程序的内存四区模型
spacy中en_core_web_sm安装问题
Work report of epidemic data analysis platform [4] cross domain correlation
JSP implementation of bank counter business performance evaluation system
Construction case of Expressway Precast Beam Yard (with scheme text)
疫情数据分析平台工作报告【6.5】疫情地图
后续版本是否会支持代码块搜索高亮显示
疫情数据分析平台工作报告【4】跨域相关
Eight fallacies of distributed computing
E-commerce middle office system architecture
[Yugong series] March 2022 asp Net core Middleware - cross domain
Dynamic gauge (15) - Minimum toll
Pat class B 1067 trial password (20 points)
1. Mx6ull learning notes (III) - busybox creates root file system
Naive Bayes classification of scikit learn
FOB,CIF,CFR的区别是什么?
The solution to the error "xxx.pri has modification time XXXX s in the futrue" in the compilation of domestic Kirin QT