decode

DECODE ( expression, search, result [ , search, result ]...[, default ] )

DECODE is similar to the IF-THEN-ELSE and CASE expressions:

CASE expression
[WHEN search THEN result]
[WHEN search THEN result]
...
[ELSE default];

decode(条件,1,翻译值1,2,翻译值2,...n,翻译值n,缺省值)的理解如下:

if (条件==1)then    return(翻译值1)

elsif (条件==2)then return(翻译值2)    

......

elsif (条件==n)then return(翻译值n)

else return(缺省值)

end if

示例:

SELECT product_description
       , DECODE(weight ,2, 'Light' 
                       ,50, 'Medium'
                       ,71, 'Heavy',
                       ,99, 'Call for help'
                       ,'N/A') 

相当于

 

SELECT product_description
       , case weight when 2 then 'Light' 
                     when 50 then 'Medium'
                     when 71 then 'Heavy',
                     when 99 then 'Call for help'
                     else 'N/A'
         end as weight

 

 一种计数方法:

status为active的总数

sum(decode(status,'active',1,0)) as active

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值