含**釋: 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
使用方法: 1、比較大小
select decode(sign(變數1-變數2),-1,變數1,變數2) from dual; --取較小值
sign()函式根據某個值是0、正數還是負數,分別返回0、1、-1
例如:
變數1=10,變數2=20
則sign(變數1-變數2)返回-1,decode解碼結果為「變數1」,達到了取較小值的目的。
2、表、檢視結構轉化
現有乙個商品銷售表sale,表結構為:
month char(6) --月份
sell number(10,2) --月銷售金額
現有資料為:
200001 1000
200002 1100
200003 1200
200004 1300
200005 1400
200006 1500
200007 1600
200101 1100
200202 1200
200301 1300
想要轉化為以下結構的資料:
year char(4) --年份
month1 number(10,2) --1月銷售金額
month2 number(10,2) --2月銷售金額
month3 number(10,2) --3月銷售金額
month4 number(10,2) --4月銷售金額
month5 number(10,2) --5月銷售金額
month6 number(10,2) --6月銷售金額
month7 number(10,2) --7月銷售金額
month8 number(10,2) --8月銷售金額
month9 number(10,2) --9月銷售金額
month10 number(10,2) --10月銷售金額
month11 number(10,2) --11月銷售金額
month12 number(10,2) --12月銷售金額
結構轉化的sql語句為:
create or replace view
v_sale(year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)
asselect
substrb(month,1,4),
sum(decode(substrb(month,5,2),01,sell,0)),
sum(decode(substrb(month,5,2),02,sell,0)),
sum(decode(substrb(month,5,2),03,sell,0)),
sum(decode(substrb(month,5,2),04,sell,0)),
sum(decode(substrb(month,5,2),05,sell,0)),
sum(decode(substrb(month,5,2),06,sell,0)),
sum(decode(substrb(month,5,2),07,sell,0)),
sum(decode(substrb(month,5,2),08,sell,0)),
sum(decode(substrb(month,5,2),09,sell,0)),
sum(decode(substrb(month,5,2),10,sell,0)),
sum(decode(substrb(month,5,2),11,sell,0)),
sum(decode(substrb(month,5,2),12,sell,0))
from sale
group by substrb(month,1,4);
select v_dept_code as 機構**,
sum(decode(n_type, 1, n_money_left,0)) as 長款金額,
sum(decode(n_type, 1, 1,0)) as 長款筆數,
sum(decode(n_type, 2, n_money_left,0)) as 短款金額,
sum(decode(n_type, 2, 1,0)) as 短款筆數
from tb_cdk_fund_danger_info t
where d_confirm_date between sysdate - 1 and sysdate
group by v_dept_code
decode函式用法
decode 函式將該數值與一系列的序偶相比較,以決定最後的返回結果 序偶序偶,就是成對出現 我們將這個函式與 switch 比較,我們發現他們基本相似,唯一不同的是 switch 將要比較的值放在 case 後面都加上了 break中。select id,decode flag,y yes n n...
Decode函式用法
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...
decode函式的用法
decode的作用當字段或字段的運算的值等於值1時,該函式返回值2,否則返回值3 含 釋 decode 條件,值1,返回值1,值2,返回值2,值n,返回值n,預設值 該函式的含義如下 if 條件 值1 then return 翻譯值1 elsif 條件 值2 then return 翻譯值2 els...