oracle中的函数

来源:互联网 发布:开淘宝店怎么寻找货源 编辑:程序博客网 时间:2024/05/16 14:10

case 函数

1、简单case函数

例1:

case sex

when '1'  then '男'

when '2' then '女'

else '其他' end

例2:

case when sex='1' then '男'

          when sex='2' then '女'

          else '其他' end

例3:

select (case when citynum='1' then '市中公司'
                     when citynum='2' then '东昌府'
                     when citynum='11' then '客服中心'
                     else '其他县区'
                     end ) as myCity
from t_city

例4:
select count(*)
from t_city group by
case when citynum='1' then '市中公司'
                     when citynum='2' then '东昌府'
                     when citynum='11' then '客服中心'
                     else '其他县区'
                     end

一般语法:

select (

case when A then sthA when B then sthB when C  then stnC else sthE end

) as myColoum from t_table

 

decode函数

语法:decode(value,if1,then1,if2,then2,……else)

1)value代表某个表的任何类型的任意列,或一个通过计算所得的任何结果

2)当每个value值被测试,如果值为if1,decode函数的结果是then1,如果value的值为if2,则decode函数的结果是then2

3)可以给出多个if/then配对

4)如果value不等于任何给出的配对,则返回else

例:

sum(decode(progress,'归档','1','临时归档','1',0))

即:如果progress是‘归档’或‘临时归档’,则统计结果加1,否则不加

 

sign(n)函数

作用:取数字n的符号,大于0返回1,小于0返回-1,等于0返回0,可以用来判断两个变量的大小

例:

select decode(sign(变量1-变量2),-1,变量1,变量2)

即:变量1减去变量2,如果结果小于零,则返回-1,那么查询结果返回变量1,否则返回变量2,也就是比较了两个变量,返回较小的变量

 

0 0
原创粉丝点击