Oracle Decode()函数使用技巧分享

今天上头要求做一个类似的功能,初步想到的列转行,但是如何实现也没有什么好办法,这个函数不错

decode函数是oracle pl/sql是功能强大的函数之一,目前还只有oracle公司的sql提供了此函数,其他数据库厂商的sql实现还没有此功能。decode有什么用途 呢?

先构造一个例子,假设我们想给智星职员加工资,其标准是:工资在8000元以下的将加20%;工资在8000元以上的加15%,通常的做法是,先选出记录 中的工资字段值? select salary into var-salary from employee,然后对变量var-salary用if-then-else或choose case之类的流控制语句进行判断。

如果用decode函数,那么我们就可以把这些流控制语句省略,通过sql语句就可以直接完成。如下:select decode(sign(salary – 8000),1,salary*1.15,-1,salary*1.2,salary from employee 是不是很简洁?

decode的语法:decode(value,if1,then1,if2,then2,if3,then3,…,else),表示如果value 等于if1时,decode函数的结果返回then1,…,如果不等于任何一个if值,则返回else。初看一下,decode 只能做等于测试,但刚才也看到了,我们通过一些函数或计算替代value,是可以使decode函数具备大于、小于或等于功能。

decode(条件,值1,翻译值1,值2,翻译值2,…值n,翻译值n,缺省值)

decode(字段,比较1,值1,比较2,值2,…..,比较n,值n缺省值)  

该函数的含义如下:
if 条件=值1 then
    return(翻译值1)
elsif 条件=值2 then
    return(翻译值2)
    ……
elsif 条件=值n then
    return(翻译值n)
else
    return(缺省值)
end if

  decode()函数使用技巧

  ·软件环境:

  1、windows nt4.0+oracle 8.0.4

  2、oracle安装路径为:c:/orant

  · 使用方法:

  1、比较大小

  select decode(sign(变量1-变量2),-1,变量1,变量2) from dual; –取较小值

  sign()函数根据某个值是0、正数还是负数,分别返回0、1、-1

  例如:

  变量1=10,变量2=20

  则sign(变量1-变量2)返回-1,decode解码结果为“变量1”,达到了取较小值的目的。

  2、表、视图结构转化

  现有一个商品销售表sale,表结构为:

  month    char(6)      –月份

  sell    number(10,2)   –月销售金额

  现有数据为:

  200001  1000

  200002  1100

  200003  1200

  200004  1300

  200005  1400

  200006  1500

  200007  1600

  200101  1100

  200202  1200

  200301  1300

  想要转化为以下结构的数据:

  year   char(4)      –年份

  month1  number(10,2)   –1月销售金额

  month2  number(10,2)   –2月销售金额

  month3  number(10,2)   –3月销售金额

  month4  number(10,2)   –4月销售金额

  month5  number(10,2)   –5月销售金额

  month6  number(10,2)   –6月销售金额

  month7  number(10,2)   –7月销售金额

  month8  number(10,2)   –8月销售金额

  month9  number(10,2)   –9月销售金额

  month10  number(10,2)   –10月销售金额

  month11  number(10,2)   –11月销售金额

  month12  number(10,2)   –12月销售金额

  结构转化的sql语句为:

  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)),

 

======================================================

补充1:

有学生成绩表student,现在要用decode函数实现以下几个功能:成绩>85,显示优秀;>70显示良好;>60及格;否则是不及格。
假设student的编号为id,成绩为score,那么:
select id, decode(sign(score-85),1,’优秀’,0,’优秀’,-1,
decode(sign(score-70),1,’良好’,0,’良好’,-1,
decode(sign(score-60),1,’及格’,0,’及格’,-1,’不及格’)))
from student;

======================================================
补充2:
decode函数的语法结构如下:
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函数比较表达式和搜索字,如果匹配,返回结果;如果不匹配,返回default值;如果未定义default值,则返回空值。
以下是一个简单测试,用于说明decode函数的用法:
sql> create table t as select username,default_tablespace,lock_date from dba_users;

table created.

sql> select * from t;

username                        default_tablespace              lock_date
—————————— —————————— ———
sys                             system
system                          system
outln                           system
csmig                           system
scott                           system
eygle                           users
dbsnmp                          system
wmsys                           system                          20-oct-04

8 rows selected.


sql> select username,decode(lock_date,null,’unlocked’,’locked’) status from t;

username                        status
—————————— ——–
sys                             unlocked
system                          unlocked
outln                           unlocked
csmig                           unlocked
scott                           unlocked
eygle                           unlocked
dbsnmp                          unlocked
wmsys                           locked

8 rows selected.

sql> select username,decode(lock_date,null,’unlocked’) status from t;

username                        status
—————————— ——–
sys                             unlocked
system                          unlocked
outln                           unlocked
csmig                           unlocked
scott                           unlocked
eygle                           unlocked
dbsnmp                          unlocked
wmsys

8 rows selected.

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐