[Oracle] decode 函数及其用法

时间:2022-12-26 23:01:42

http://blog.csdn.net/oscar999/article/details/18399177

前言

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)

比较表达式和搜索字,如果匹配,返回结果;如果不匹配,返回default值;如果未定义default值,则返回空值。

实例使用

1. 创建一个学生成绩表。栏位有: 学生名, 科目名和分数

    1. create table student_score(
    2. name varchar2(30),
    3. subject varchar2(20),
    4. score number(4,1)
    5. );

2. 插入测试数据

  1. insert into student_score (name,subject,score)values('zhang san','Chinese',90);
  2. insert into student_score (name,subject,score)values('zhang san','Mathematics',80);
  3. insert into student_score (name,subject,score)values('zhang san','English',79);

3.测试一:

  1. select name,subject,decode(subject, 'Chinese',score,0) from student_score;

结果如下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZcAAABUCAIAAADBD+30AAAMMElEQVR4nO2d36vcxhXH519aiBLXJiFwi8HgQkBgEC63+eWHhLbOXQIBi5gb+pASChahrUP3LeAktE2hu3kI7kO60JpwSWNokpo6doux6aX27k1aN43TuN0+SJpfOqMdSauZ0cz5PCR7d0ea853R+WpGd+8x2UUQBBkyZGmKlS/s7u7aDsECYapG3GR3d5f3FqMuZqyvXsnz2XYUpglTNeImll3s57/43dOnXnvyyfNPPP6T72y/evLb2Rtv/NZYDBuB5rMHWvQJUzXiJpZd7NTTr61Wq/v3/3vvq6+/+Nd/Pvv83okTr+zt7RkLozs0nz3Qok+YqhE3sexiTz310/v3/3f27C4h5MyZs7dvfxHHL1+4cMFYGN2h+eyBFn3CVI24iWU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CLdcUnLfqEqRpxE3SxrvikRZ8wVSNugi7WFZ+06BOmasRNenUxZXmqmhwYZt19+W+sW0hQlGDVqpFvftDQxRBb2Kq7r6xYDTLIuvuK+m1NMX9gO9DFECtYrLsv18miOeBP3f3SR6QahU0jlMwIXqBBYYOD1h/oYogVrNTdzxNOXiZwLuZL3X1WmEwwkaYRgkuqirUBYYOD1h/oYogVbNXdX1XrVks54EPdfWgt1iJCHRcDwwYHrT/QxRArWHQxOVmFHPCj7r7CgHpxsWrY6GJIGJjeUXJ1sdVrMW/q7otew/3TRM0i1HAxuBw+uhgSAqbr7qu/aOFd3X3gmxbFP+qhHSFYDn/9m8LB1bL9vYEuhtgB6+73QTctJiLsA3QxxB18dDH36+6bjbAP0MUQd/DRxczikxZ9wlSNuAm6WFd80qJPmKoRN0EX64pPWvQJUzXiJuhiXfFJiz5hqkbcRHaxJYIgyJBBF0MQZNigiyEIMmzQxRAEGTb/Bx/5iv6ugcQ1AAAAAElFTkSuQmCC" alt="" />

如果是中文课程的话, 显示分数, 其他课程分数为零。

这条SQL 看上去使用意义不大。

测试二:

  1. select name,sum(decode(subject, 'Chinese',score,0)) as CHINESE from student_score group by name;

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAALgAAAAmCAIAAAD4PcajAAAFV0lEQVR4nO2a32sUVxTHz7804NU0QRFSAoEUhAuBIWXrrzwotnEHQchgWOmDRQoZpG2EeROi0mqhuz5I+mAX2iCLNVC1oTFpCQldGmdW29QaNe324c6cufNjNzOTHefOON+X3Nm5O3O+537uuXc2A4uFCoUQVAoVCiEwBVA7L6pUKmmH0GNVKhU2RqKAknYIvREDJe0oeikRQfnyq++Pj18+enTmyOHPPyhdGntfu3r1u7RDiyYEJQdemEQEZfz45Xa7vb3979bL18//fvX02dbo6MVGo5F2dBGEoOTAC5OIoBw79sX29n9TUxUAmJyc2th4TumF2dnZtKOLIAQlB16YRATlyOHPtl68BoCnrRcAsL7+16FDH2cruQhKDrwwiQhKqXRpc/Ml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5O6rLtV0xQrHN1LmSVAGKO7Ou6Mwnwerrs6+nMkZ1XMSd3zoVC38jnRXC5KoqNO1+8sS0iKNiFn/eBmPO1hxs/V5P95cbRN6oecRflbhnuRh28CCx0RgjJIii67FkdAkDhR9wzaLsBxR1ktBsFesmIsrf08DW/S/T8NVTSdaLjkFt7iy6guDd1UW/k95IR8R7rqmw1nVaKTz2+ZzJMLnfe6mLVR6J6HtywJ9tVyjpeXNbdPZ2bErXrA5RrJ+3aIYW7kceL4OLTEvypPZvfwh/cvLM/CWUFlPB6a0Dh6tgb+EGmACXZUNIOoWfKkxcmBOV/yKmVdywSwfEAAAAASUVORK5CYII=" alt="" />

统计中文课程的分数。看上去有点意义。

总体看来, decode 的使用看上去和case when 有点类似。如果只是用作以上两种状况,看上去意义不大。

  1. select name,sum(decode(subject, 'Chinese',score,0)) as CHINESE from student_score group by name;
  2. select name,score as CHINESE from student_score;

使用的两句使用后的效果一样,看上去使用decode 多此一举。

行转列-有意义的使用

往以上table 再插入一些其他学生的成绩:

  1. insert into student_score (name,subject,score)values('li shi','Chinese',96);
  2. insert into student_score (name,subject,score)values('li shi','Mathematics',86);
  3. insert into student_score (name,subject,score)values('li shi','English',76);
  4. insert into student_score (name,subject,score)values('wang wu','Chinese',92);
  5. insert into student_score (name,subject,score)values('wang wu','Mathematics',82);
  6. insert into student_score (name,subject,score)values('wang wu','English',72);

使用以下SQL:

  1. select name,
  2. sum(decode(subject, 'Chinese', nvl(score, 0), 0)) "Chinese",
  3. sum(decode(subject, 'Mathematics', nvl(score, 0), 0)) "Mathematics",
  4. sum(decode(subject, 'English', nvl(score, 0), 0)) "English"
  5. from student_score
  6. group by name;

返回:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATcAAABVCAIAAACjCzSAAAAL7ElEQVR4nO1c74sctxnWn9SFTOLaJARcDAYXUgYOBpdrfvlDQlvHSyDgIeZCP6SEwi2hrQP7LeAktEmgu/kQ3A/pQmvCkcbQJDV17BZj06P27jmtm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ghRhOjSpiDAhQAFe4QoQnRpUxDgQoCCPUIUQXLpIiIiwm/EsbQOBLgQoGCPEEWILm0KAlwIULBHiCJElzYFAS4EKNgjRBGiS5uCABcCFOwRogjRpU1BgAsBCvYIUQQnLkW/6bx8unyF9k0+sJl7/SINN7Z9Zj18ji6a9wgcr7Yp7kft+gGe96jwVwRj3qMCq7O1S+FWIUNGFV62S/P0nDbJm+iqQDPlFPKOvBLeUrAHmveo8FoEPO9RYaizwxmvuhu73qXGAVYHlrioNm3SN2gsRTPloNsWvaVgDzTvUSgiSCY11NmJS0sbqd5oMpY2GRLRxEVo2qTmz1wXHka3BJopZ5ZnWabOg72l4ADYVtJQRJDzHuF1djuWrj3jbeQo7M0XzZexxjPXhIfR5UAy5YD39qoP85iCLdC8R2GIIE93TXV2ucarjGrOXCoAEqlEl+oQxGF/KWYiQVBoBzTvURgiyFNcU51tXQq7sW7GUiRxURFdqkFaOAFCiWbrP4XWQPMeBSGC+h5qqLODNV7DerJQRF50XjoZPbiqADAb4MvucMGo+TPXhm/RFcDzHkkrbsuoeEvBAQyN0HsRkGRAaJ3jtxoagQAXAhTsEaII0aVNQYALAQr2CFGE6NKmIMCFAAV7hChCdGlTEOBCgII9QhQhurQpCHAhQMEeIYoQXdoUBLgQoGCPEEWQXLqIiIjwG9GlERG+I7o0IsJ3/B+JS3NSY0mmbgAAAABJRU5ErkJggg==" alt="" />

将行的数据转化为列, 是不是很有意义了。

使用case then 也可以达到相同的效果。

    1. select name,
    2. sum(case when subject='Chinese'
    3. then nvl(score,0)
    4. else 0
    5. end) "Chinese",
    6. sum(case when subject='Mathematics'
    7. then nvl(score,0)
    8. else 0
    9. end) "Mathematics",
    10. sum(case when subject='English'
    11. then nvl(score,0)
    12. else 0
    13. end) "English"
    14. from student_score
    15. group by name;

[Oracle] decode 函数及其用法的更多相关文章

  1. Oracle DECODE函数的用法详解

    Oracle DECODE函数使用方法: 1.比较大小select decode(sign(变量1-变量2),-1,变量1,变量2) from dual; --取较小值sign()函数根据某个值是0. ...

  2. oracle decode函数的用法

    含义解释: decode(字段,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 用法如下:IF 字段=值1 返回 返回值1ELSIF 字段=值2 返回 返回值2 ......ELSIF ...

  3. Oracle DECODE函数的语法介绍

    Oracle DECODE函数功能很强,下面就为您详细介绍Oracle DECODE函数的用法,希望可以让您对Oracle DECODE函数有更多的了解. Oracle DECODE函数 Oracle ...

  4. Oracle中NVL、NVL2、DECODE函数的用法

    DECODE函数的用法:   DECODE(value,if1,then1,if2,then2,if3,then3,......,else),表示如果value的值等于if1时,DECODE函数的结果 ...

  5. Oracle to_date()函数的用法

    Oracle to_date()函数的用法 to_date()是Oracle数据库函数的代表函数之一,下文对Oracle to_date()函数的几种用法作了详细的介绍说明,供您参考学习. 在Orac ...

  6. Oracle trunc()函数的用法

    Oracle trunc()函数的用法 /**************日期********************/1.select trunc(sysdate) from dual --2013-0 ...

  7. oracle获取本月第一天和最后一天及Oracle trunc()函数的用法

    select to_char(trunc(add_months(last_day(sysdate), -1) + 1), 'yyyy-mm-dd') "本月第一天", to_cha ...

  8. Oracle trunc()函数的用法--来着心静禅定ing

    1.TRUNC(for dates) TRUNC函数为指定元素而截去的日期值. 其具体的语法格式如下: TRUNC(date[,fmt]) 其中: date 一个日期值 fmt 日期格式,该日期将由指 ...

  9. Oracle trunc()函数的用法及四舍五入 round函数

    --Oracle trunc()函数的用法/**************日期********************/1.select trunc(sysdate) from dual  --2011 ...

随机推荐

  1. Oracle 创建 split 和 splitstr 函数

    Sql语句最好依次执行创建 /************************************** * name:        split * author:      sean zhang ...

  2. Bootstrap学习笔记上(带源码)

    阅读目录 排版 表单 网格系统 菜单.按钮 做好笔记方便日后查阅o(╯□╰)o bootstrap简介: ☑  简单灵活可用于架构流行的用户界面和交互接口的html.css.javascript工具集 ...

  3. 6、JPA_映射单向多对一的关联关系(n的一方有1的引用,1的一方没有n的集合属性)

    单向多对一的关联关系 具体体现:n的一方有1的引用,1的一方没有n的集合属性 举个例子:订单Order对顾客Customer是一个单向多对一的关联关系.Order是n的一方,有对Customer的引用 ...

  4. 三年PS经验

  5. DDL

    在DDL(Data Definition Language)中,创建.删除.修改使用create.drop.alter关键字 数据库的创建 create database 数据库名选择数据库 use ...

  6. 【Docker】(2)---仓库、镜像、容器

    [Docker](2)---仓库.镜像.容器     学习Docker,我觉得首先要了解的是仓库.镜像.容器到底是什么,他们有什么区别. 一.通俗理解 1.Docker 镜像 (images) 容器运 ...

  7. Day05 -寻找自己:Ruby的self物件与singleton method

    前情提要:在第四天里,我们用参赛者的例子解说实体方法与类别方法.类别中的实体物件,想要玩弄方法时,可以有三种取用方式:(跟斯斯有三种一样) 该类别所定义的实体方法. 模块中可取得的实体方法.(关于模块 ...

  8. Maths | 层次分析法(Analytic Hierarchy Process)

    目录 1. 概述 2. AHP算法 2.1. 建立层级 2.2. 构造 成对 比较 矩阵 2.3. 成对比较矩阵的 一致性检验 与 层次单排序 2.4. 层次总排序 参考: (中文)https://z ...

  9. Python自动化开发 - RESTful API

    本节内容 1.  RESTful 简介 2.  RESTful 设计指南 3.  Django REST Framework 最佳实践 4.  理论拓展与开放平台 5.  API文档化与测试 一  R ...

  10. 15-(基础入门篇)GPRS(Air202)GPIO控制点亮一个灯

    https://www.cnblogs.com/yangfengwu/p/9967027.html 现在点亮它,说一下哈,都过去好久了,不知道大家都在用哪个版本的库 http://www.openlu ...