decode函数详解及使用实例(原创)_第1页
decode函数详解及使用实例(原创)_第2页
decode函数详解及使用实例(原创)_第3页
decode函数详解及使用实例(原创)_第4页
decode函数详解及使用实例(原创)_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

decode 函数实例详解及使用函数实例详解及使用 decode 函数实例详解函数实例详解 已知学生表 学号已知学生表 学号 姓名姓名 性别 性别 查询原始数据如下 查询原始数据如下 selectselect fromfrom t student t student S IDS IDS NAMES NAMESEXSEX 10011001a a1 1 10021002b b2 2 10031003c c1 1 10041004d d 10051005e e2 2 10061006f f3 3 查询 查询 selectselect s decode s sex s decode s sex 1 1 男男 其它其它 asas new sexnew sex fromfrom t studentt student s s 返回 返回 S IDS IDS NAMES NAMESEXSEXNEW SEXNEW SEX 10011001a a1 1 男男 10021002b b2 2 其它其它 10031003c c1 1 男男 10041004d d 其它其它 10051005e e2 2 其它其它 10061006f f3 3 其它其它 解释 解释 decode s sex decode s sex 1 1 男男 其它其它 解释 如果解释 如果 s sexs sex 的值为的值为 1 1 则返回则返回 男男 否则否则 返回返回 其它其它 查询 查询 selectselect s s decode s sex decode s sex 1 1 男男 2 2 女女 其它其它 asas new sexnew sex fromfrom t studentt student s s S IDS IDS NAMES NAMESEXSEXNEW SEXNEW SEX 10011001a a1 1 男男 10021002b b2 2 女女 10031003c c1 1 男男 10041004d d 其它其它 10051005e e2 2 女女 10061006f f3 3 其它其它 解释 解释 decode s sex decode s sex 1 1 男男 其它其它 解释 如果解释 如果 s sexs sex 的值为的值为 1 1 则返回则返回 男男 如果如果 s sexs sex 的值为的值为 2 2 则返回则返回 女女 否则返回否则返回 其它其它 小结小结 decodedecode 语法 语法 Decode Decode 变量变量 值值 1 1 返回值返回值 a a 值值 2 2 返回值返回值 b b 返回值返回值 n n 解释 如果变量解释 如果变量 值值 1 1 则返回值则返回值 a a 变量变量 值值 2 2 则返回值则返回值 b b 配对出现配对出现 最后最后 一个一个 否则返回值否则返回值 n n 即 即 decodedecode 中间的条件只要配对出现中间的条件只要配对出现 则个数不限 直到最后有一个单独则个数不限 直到最后有一个单独 的返回值的返回值 表示不满足前面所有的条件时所返回的值 表示不满足前面所有的条件时所返回的值 decode 函数在统计中的使用 查询男女的人数函数在统计中的使用 查询男女的人数 通常是这样实现的 通常是这样实现的 selectselect decode s sex decode s sex 1 1 男男 2 2 女女 其它其它 asas new sex new sex count count x x fromfrom t studentt student s s groupgroup byby decode s sex decode s sex 1 1 男男 2 2 女女 其它其它 NEW SEXNEW SEXX X 男男 2 2 女女 2 2 其它其它 2 2 但如果企业要求这样的格式但如果企业要求这样的格式 怎么办呢怎么办呢 男男女女其它其它 2 22 21 1 则必须这样做 则必须这样做 1 1 首先通过 首先通过 decodedecode 函数将各种性别人员分开 函数将各种性别人员分开 selectselect s sex s sex decode s sex decode s sex 1 1 1 1 null null asas 男男 decode s sex decode s sex 2 2 1 1 null null asas 女女 decode s sex decode s sex 3 3 1 1 null null asas 其它其它 fromfrom t studentt student s s SEXSEX 男男女女其它其它 1 11 1 2 2 1 1 1 11 1 2 2 1 1 3 3 1 1 理解说明 理解说明 decode s sex decode s sex 1 1 1 1 null null 表示如果性别 表示如果性别 1 1 则返回则返回 1 1 由于是计数由于是计数 只要返回一个非空值即可只要返回一个非空值即可 2 2 计数 计数 selectselect count decode s sex count decode s sex 1 1 1 1 null null asas 男男 count decode s sex count decode s sex 2 2 1 1 null null asas 女女 count decode s sex count decode s sex 3 3 1 1 null null asas 其它其它 fromfrom t studentt student s s 说明 注意字段名为说明 注意字段名为 男男 字段名称是没有用引号的 字段名称是没有用引号的 男男女女其它其它 人数人数 2 22 21 1 示例脚本示例脚本 createcreate tabletable t student t student s ids id number number 4 4 primaryprimary key key s names name varchar2 varchar2 8 8 sexsex char char 1 1 insertinsert intointo t student s id s name sex t student s id s name sex values values 10011001 a a 1 1 insertinsert intointo t student s id s name sex t student s id s name sex values values 10021002 b b 2 2 insertinsert intointo t student s id s name sex t student s id s name sex values values 10031003 c c 1 1 insertinsert intointo t student s id s name sex t student s id s name sex values values 10041004 d d null null insertinsert intointo t student s id s name sex t student s id s name sex values values 10051005 e e 2 2 insertinsert intointo t student s id s name sex t student s id s name sex values values 10061006 f f 3 3 commit commit SELECTSELECT FROMFROM t student 男女其它 2 2 1 SELECTSELECT FROMFROM t student SELECTSELECT SELECTSELECT COUNTCOUNT FROMFROM t student WHEREWHERE sex 1 ASAS 男 SELECTSELECT COUNTCOUNT FROMFROM t student WHEREWHERE sex 2 ASAS 女 SELECTSELECT COUNTCOUNT FROMFROM t student WHEREWHERE sex 3 OROR sex ISIS NULLNULL ASAS 其它 FROMFROM dual SELECTSELECT t decode t sex 1 1 NULLNULL 男 decode t sex 2 1 NULLNULL 女 decode t sex 1 NULLNULL 2 NULLNULL 1 其它 FROMFROM t student t 子查询 SELECTSELECT 人数 ASAS title COUNTCOUNT sex1 男 COUNTCOUNT sex2 女 COUNTCOUNT sex3 其它 COUNTCOUNT 合计 FROMFROM SELECTSELECT t decode t sex 1 1 NULLNULL sex1 decode t sex 2 1 NULLNULL sex2 decode t sex 1 NULLNULL 2 NULLNULL 1 sex3 FROMFROM t student t t 或者 SELECTSELECT countcount decode t sex 1 1 NULLNULL 男 countcount decode t sex 2 1 NULLNULL 女 countcount decode t sex 1 NULLNULL 2 NULLNULL 1 其 它 FROMFROM t stude

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论