decode函数详解及使用实例()

上传人:积*** 文档编号:121150052 上传时间:2022-07-18 格式:DOC 页数:7 大小:57.50KB
返回 下载 相关 举报
decode函数详解及使用实例()_第1页
第1页 / 共7页
decode函数详解及使用实例()_第2页
第2页 / 共7页
decode函数详解及使用实例()_第3页
第3页 / 共7页
点击查看更多>>
资源描述
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(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 tGROUP BY t.deptno
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 考试试卷


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!