国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > Oracle > 正文

Oracle中Decode()函數使用技巧

2024-08-29 13:30:12
字體:
來源:轉載
供稿:網友
decode()函數使用技巧
·軟件環境:
1、windows nt4.0+oracle 8.0.4
2、oracle安裝路徑為:c:/orant
·含義解釋:
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)
as
    select
    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);
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 垦利县| 镇赉县| 建阳市| 如东县| 巴楚县| 晋江市| 平邑县| 荣昌县| 海伦市| 乐亭县| 来凤县| 恭城| 怀化市| 义乌市| 昌乐县| 汉川市| 邵武市| 北海市| 栾川县| 独山县| 武定县| 泽州县| 达日县| 浏阳市| 饶平县| 托克托县| 志丹县| 原平市| 和田县| 湖口县| 自治县| 昌黎县| 兴隆县| 衡水市| 黎平县| 彩票| 旬邑县| 阿尔山市| 焦作市| 普洱| 碌曲县|