【函数格式】:

        decode (

        expression,

        condition_01, result_01,

        condition_02, result_02,

        ......,

        condition_n, result_n,

        result_default)

【函数说明】:

  • 若表达式expression值与condition_01值匹配,则返回result_01,若不匹配,则继续判断;
  • 若表达式expression值与condition_02值匹配,则返回result_02,若不匹配,则继续判断;
  • 以此类推,若表达式expressioncondition_n值匹配,则返回result_n,若不匹配,则继续判断;
  • 若表达式expression值与以上所有的condition都不匹配,则返回默认值result_default,若省略result_default参数,则返回null

【函数解释】:

        其实,该函数的作用可以用以下一段if else条件语句进行更加形象地解释:
        if expression=condition_01 then

                return result_01;

        else

               if expression=condition_02 then

                        return result_02;

                else

                        ......

                        if expression=condition_n then

                                return result_n;

                        else

                                return result_default;

                        end if;

                end if;

        end if;

【使用形式】:

        因为该函数的匹配条件condition可以设置一个或多个,默认值result_default也可以省略,因此该函数具体的使用形式有以下几种:

  • decode (expression,condition_01,result_01)

        释义:expression与condition_01匹配,则返回result_01,否则返回null

  • decode (expression,condition_01,result_01,result_default)

        释义:expression与condition_01匹配,则返回result_01,否则返回result_default

  • decode (expression,condition_01,result_01,condition_02,result_02)

        释义:expression与condition_01匹配,则返回result_01,若不匹配,则继续判断,若expression与condition_02匹配,则返回result_02,否则返回null

  • decode (expression,condition_01,result_01,condition_02,result_02,result_default)

        释义:expression与condition_01匹配,则返回result_01,若不匹配,则继续判断,若expression与condition_02匹配,则返回result_02,否则返回result_default

  • decode(expression,condition_01,result_01,condition_02,result_02,......,condition_n,result_n)

        释义:expression与condition_01匹配,则返回result_01,若不匹配,则继续判断,若expression与condition_02匹配,则返回result_02,若不匹配,则继续判断,直到expression与condition_n匹配为止,返回result_n否则返回null

  • decode(expression,condition_01,result_01,condition_02,result_02,......,condition_n,result_n,result_default)

        释义:expression与condition_01匹配,则返回result_01,若不匹配,则继续判断,若expression与condition_02匹配,则返回result_02,若不匹配,则继续判断,直到expression与condition_n匹配为止,返回result_n否则返回result_default

【使用场景】:

创建演示数据

先建一张学生成绩表,建表语句如下所示:

-- Create table
create table T_STUDENT_GRADE
(
  studentid    VARCHAR2(50) not null,
  classid      VARCHAR2(50),
  engilshgrade NUMBER,
  chinesegrade NUMBER,
  mathgrade    NUMBER,
  gradelevel   VARCHAR2(50)
)
tablespace STUDINFO
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table T_STUDENT_GRADE
  is '学生成绩表(演示使用)';
-- Add comments to the columns 
comment on column T_STUDENT_GRADE.studentid
  is '学生id';
comment on column T_STUDENT_GRADE.classid
  is '班级id';
comment on column T_STUDENT_GRADE.engilshgrade
  is '英语成绩';
comment on column T_STUDENT_GRADE.chinesegrade
  is '语文成绩';
comment on column T_STUDENT_GRADE.mathgrade
  is '数学成绩';
comment on column T_STUDENT_GRADE.gradelevel
  is '成绩分类';
-- Create/Recreate primary, unique and foreign key constraints 
alter table T_STUDENT_GRADE
  add constraint PK_T_STUDENT_GRADE primary key (STUDENTID)
  using index 
  tablespace STUDINFO
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );

再往表中插入数据,插入语句如下所示:

insert into T_STUDENT_GRADE (studentid, classid, engilshgrade, chinesegrade, mathgrade, gradelevel)
values ('stud01', 'clas01', 60, 70, 80, 'C');
insert into T_STUDENT_GRADE (studentid, classid, engilshgrade, chinesegrade, mathgrade, gradelevel)
values ('stud02', 'clas01', 75, 85, 95, 'B');
insert into T_STUDENT_GRADE (studentid, classid, engilshgrade, chinesegrade, mathgrade, gradelevel)
values ('stud03', 'clas01', 80, 90, 100, 'A');
insert into T_STUDENT_GRADE (studentid, classid, engilshgrade, chinesegrade, mathgrade, gradelevel)
values ('stud04', 'clas02', 55, 60, 65, 'D');
insert into T_STUDENT_GRADE (studentid, classid, engilshgrade, chinesegrade, mathgrade, gradelevel)
values ('stud05', 'clas02', 40, 50, 60, 'E');
insert into T_STUDENT_GRADE (studentid, classid, engilshgrade, chinesegrade, mathgrade, gradelevel)
values ('stud06', 'clas02', 100, 100, 100, 'S');
commit;

查询表中数据,查询结果如下所示:

1.将指定数据翻译或转换成其他形式

sql语句如下:

select GradeLevel,decode(GradeLevel,'S','完美','A','优秀','B','良好','C','较好','D','及格','E','不及格','其他') as gradeRemark from T_STUDENT_GRADE

返回结果如下图所示:

2.对指定数据按指定范围进行分段

sql语句如下:

 select mathgrade,decode(mathgrade,100,'完美',decode(sign(mathgrade-90),1,'优秀',0,'优秀',-1,decode(sign(mathgrade-80),1,'良好',0,'良好',-1,decode(sign(mathgrade-70),1,'较好',0,'较好',-1,decode(sign(mathgrade-60),'1','及格',0,'及格',-1,'不及格'))))) as mathGradeRemark from T_STUDENT_GRADE

返回结果如下图所示:

3.对表中指定数据进行行列转换

sql语句如下:

select 
decode(GradeLevel,'S',StudentId) Level_S,
decode(GradeLevel,'A',StudentId) Level_A,
decode(GradeLevel,'B',StudentId) Level_B,
decode(GradeLevel,'C',StudentId) Level_C,
decode(GradeLevel,'D',StudentId) Level_D,
decode(GradeLevel,'E',StudentId) Level_E
from 
T_STUDENT_GRADE

返回结果如下图所示:

4.判断指定数据是否符合指定特征

sql语句如下:

//判断英语分数是否及格

select engilshgrade,decode(sign(engilshgrade-60),1,'大于60分',0,'等于60分','低于60分') passFlag from T_STUDENT_GRADE

返回结果如下图所示:

5.对数据按照指定要求进行排序

sql语句如下:

select * from T_STUDENT_GRADE order by decode(GradeLevel,'S',1,'A',2,'B',3,'C',4,'D',5,'E',6) asc

返回结果如下图所示:

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐