SQL函数之---DECODE函数

Decode函数是oracle/SQL提供的特有函数计算方式,语法:DECODE(value,if1,then1,if2,then2,if3,then3,...else),通常我们在写语句的时候可能会遇到多种Case,一般的处理方法是通过Case when或者if...then....else的方式实现,但是如果用decode函数,就显得简洁和方便很多,举例:

假设在table1 中有一列company的值122和123,我们可以通过以下方式,将两个值以两列的形式显示出来:

SELECT 
DECODE(company, '122', '122', ''),
DECODE(company, '123', '123', ''),

sum(field1)

FROM table1

上述语句表示,在列company中,表示if 122 then 122 else为空;同样if 123 then 123 else为空

posted @ 2015-09-24 15:58  rosewong  阅读(3308)  评论(0编辑  收藏  举报