文章詳情頁(yè)
Oracle中decode()函數(shù)的使用技巧
decode()函數(shù)的使用技巧:·軟件環(huán)境:1、windows nt4.0+Oracle 8.0.42、oracle安裝路徑為:c:orant·含義解釋:decode(條件,值1,翻譯值1,值2,翻譯值2,...值n,翻譯值n,缺省值)該函數(shù)的含義如下:if 條件=值1 thenreturn(翻譯值1)elsif 條件=值2 thenreturn(翻譯值2)......elsif 條件=值n thenreturn(翻譯值n)elsereturn(缺省值)end if· 使用方法:1、比較大小select decode(sign(變量1-變量2),-1,變量1,變量2) from dual; --取較小值sign()函數(shù)根據(jù)某個(gè)值是0、正數(shù)還是負(fù)數(shù),分別返回0、1、-1例如:變量1=10,變量2=20則sign(變量1-變量2)返回-1,decode解碼結(jié)果為“變量1”,達(dá)到了取較小值的目的。2、表、視圖結(jié)構(gòu)轉(zhuǎn)化現(xiàn)有一個(gè)商品銷售表sale,表結(jié)構(gòu)為:month char(6) --月份sell number(10,2) --月銷售金額現(xiàn)有數(shù)據(jù)為:2000011000200002110020000312002000041300200005140020000615002000071600200101110020020212002003011300想要轉(zhuǎn)化為以下結(jié)構(gòu)的數(shù)據(jù):year char(4) --年份month1number(10,2) --1月銷售金額month2number(10,2) --2月銷售金額month3number(10,2) --3月銷售金額month4number(10,2) --4月銷售金額month5number(10,2) --5月銷售金額month6number(10,2) --6月銷售金額month7number(10,2) --7月銷售金額month8number(10,2) --8月銷售金額month9number(10,2) --9月銷售金額month10number(10,2) --10月銷售金額month11number(10,2) --11月銷售金額month12number(10,2) --12月銷售金額結(jié)構(gòu)轉(zhuǎn)化的sql語(yǔ)句為:create or replace viewv_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)),
標(biāo)簽:
Oracle
數(shù)據(jù)庫(kù)
排行榜
