decode 实例

来源:互联网 发布:软件开发管理规范 编辑:程序博客网 时间:2024/06/05 01:53

以往相关材料:

http://blog.csdn.net/arrowzz/article/details/17144651

http://blog.csdn.net/arrowzz/article/details/17144669



id,name,score
1,小明,胜
2,小明,胜
3,小李,负
4,小李,负
5,小明,负
6,小李,胜
7,小李,胜


效果
name,胜,负
小明,2,1
小李,2,2


创建表:create table test20(id integer,name varchar2(20),score varchar2(20));

插入相关数据:insert into test20 values(1,'小明','胜');insert into test20 values(2,'小明','胜');insert into test20 values(3,'小李','负');insert into test20 values(4,'小李','负');insert into test20 values(5,'小明','负');insert into test20 values(6,'小李','胜');insert into test20 values(7,'小李','胜');

select语句:select name 姓名,       sum(decode(score, '胜', 1, 0)) 胜,       sum(decode(score, '负', 1, 0)) 负  from test20 group by name;


=======================================================================

==========================================================================


PS:主要使用了 decode 函数


在逻辑编程中,经常用到If – Then –Else 进行逻辑判断。

在DECODE的语法中,实际上就是这样的逻辑处理过程。

它的语法如下:


DECODE(value, if1, then1, if2,then2, if3,then3, . . . else )


Value 代表某个表的任何类型的任意列或一个通过计算所得的任何结果。

当每个value值被测试,

如果value的值为if1,Decode 函数的结果是then1;

如果value等于if2,Decode函数结果是then2;等等。


事实上,可以给出多个if/then 配对。

如果value结果不等于给出的任何配对时,Decode 结果就返回else 。


需要注意的是,这里的if、then及else 都可以是函数或计算表达式。

0 0
原创粉丝点击