RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Oracle中decode怎么用

小编给大家分享一下Oracle中decode怎么用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名申请、网络空间、营销软件、网站建设、乌恰网站维护、网站推广。

语法结构如下:
decode (expression, sch_1, res_1)
decode (expression, sch_1, res_1, sch_2, res_2)
decode (expression, sch_1, res_1, sch_2, res_2, ...., sch_n, res_n)
decode (expression, sch_1, res_1, default)
decode (expression, sch_1, res_1, sch_2, res_2, default)
decode (expression, sch_1, res_1, sch_2, res_2, ...., sch_n, res_n, default)
比较表达式和搜索字,如果匹配,返回结果;如果不匹配,返回default值;如果未定义default值,则返回空值。

select name,sub,decode(sub, 'chinese',score,0) from student_score;
decode函数的功能在于它不但可以判断常值,同样可以判断字段.以上语句:
当科目为chinese时,选择score字段对应的值,而非chinese科目,其对应的score值都为0

select name,
sum(decode(subject, 'Chinese', nvl(score, 0), 0)) "Ch",
sum(decode(subject, 'Mathematics', nvl(score, 0), 0)) "Math",
sum(decode(subject, 'English', nvl(score, 0), 0)) "En"
from xxx group by name;   ---行转列

decode函数同样可以当做条件使用,如: where score= decode(subject, 'Chinese',score)

等同于以下case when

select name,
sum(case when subject='Ch'
then nvl(score,0)
else 0
end) "Ch",
sum(case when subject='Math'
then nvl(score,0)
else 0
end) "Math",
sum(case when subject='En'
then nvl(score,0)
else 0
end) "En"
from xxx group by name;

看完了这篇文章,相信你对“Oracle中decode怎么用”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


新闻标题:Oracle中decode怎么用
本文URL:http://sczitong.cn/article/jdjges.html