decode函数详解及使用实例(原创).doc_第1页
decode函数详解及使用实例(原创).doc_第2页
decode函数详解及使用实例(原创).doc_第3页
decode函数详解及使用实例(原创).doc_第4页
decode函数详解及使用实例(原创).doc_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

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

文档简介

decode函数实例详解及使用decode函数实例详解已知学生表:学号,姓名,性别。查询原始数据如下:select * from t_student;S_IDS_NAMESEX1001a11002b21003c11004d1005e21006f3查询:select s.*,decode(s.sex,1,男,其它) as new_sex from t_student s;返回:S_IDS_NAMESEXNEW_SEX1001a1男1002b2其它1003c1男1004d其它1005e2其它1006f3其它解释:decode(s.sex,1,男,其它)解释:如果s.sex的值为1,则返回“男”,否则返回“其它”。查询:select s.*,decode(s.sex,1,男,2,女,其它) as new_sexfrom t_student s;S_IDS_NAMESEXNEW_SEX1001a1男1002b2女1003c1男1004d其它1005e2女1006f3其它解释:decode(s.sex,1,男,其它)解释:如果s.sex的值为1,则返回“男”,如果s.sex的值为2,则返回“女”,否则返回“其它”。小结decode语法:Decode(变量,值1,返回值a,值2,返回值b返回值n解释:如果变量=值1,则返回值a,变量=值2,则返回值b(配对出现),(最后一个)否则返回值n。 即:decode中间的条件只要配对出现,则个数不限。直到最后有一个单独的返回值,表示不满足前面所有的条件时所返回的值。decode函数在统计中的使用:查询男女的人数通常是这样实现的:select decode(s.sex,1,男,2,女,其它) as new_sex,count(*) xfrom t_student sgroup by decode(s.sex,1,男,2,女,其它)NEW_SEXX男2女2其它2但如果企业要求这样的格式,怎么办呢?男女其它221则必须这样做:1、首先通过decode函数将各种性别人员分开:select s.sex, decode(s.sex,1,1,null) as 男, decode(s.sex,2,1,null) as 女, decode(s.sex,3,1,null) as 其它from t_student sSEX男女其它1121112131理解说明:decode(s.sex,1,1,null):表示如果性别=1,则返回1(由于是计数,只要返回一个非空值即可)。2、计数select count(decode(s.sex,1,1,null) as 男, count(decode(s.sex,2,1,null) as 女, count(decode(s.sex,3,1,null) as 其它from t_student s说明:注意字段名为“男”,字段名称是没有用引号的。男女其它人数221示例脚本create table t_student(s_id number(4) primary key,s_name varchar2(8),sex char(1);insert into t_student(s_id,s_name,sex) values(1001,a,1);insert into t_student(s_id,s_name,sex) values(1002,b,2);insert into t_student(s_id,s_name,sex) values(1003,c,1);insert into t_student(s_id,s_name,sex) values(1004,d,null);insert into t_student(s_id,s_name,sex) values(1005,e,2);insert into t_student(s_id,s_name,sex) values(1006,f,3);commit;SELECT * FROM t_student;男女其它221SELECT * FROM t_studentSELECT (SELECT COUNT(*) FROM t_student WHERE sex=1) AS 男,(SELECT COUNT(*) FROM t_student WHERE sex =2) AS 女,(SELECT COUNT(*) FROM t_student WHERE sex =3 OR sex IS NULL) AS 其它FROM dualSELECT t.*, decode(t.sex,1,1,NULL) 男, decode(t.sex,2,1,NULL) 女, decode(t.sex,1,NULL,2,NULL,1) 其它FROM t_student t- 子查询SELECT 人数 AS title,COUNT(sex1) 男,COUNT(sex2) 女,COUNT(sex3) 其它, COUNT(*) 合计 FROM ( SELECT t.*, decode(t.sex,1,1,NULL) sex1, decode(t.sex,2,1,NULL) sex2, decode(t.sex,1,NULL,2,NULL,1) sex3 FROM t_student t) t-或者SELECT count(decode(t.sex,1,1,NULL) 男, count(decode(t.sex,2,1,NULL) 女, count(decode(t.sex,1,NULL,2,NULL,1) 其它FROM t_student t-多行-设计:各部门的男女人数SELECT t.deptno, count(deco

温馨提示

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

评论

0/150

提交评论