夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
            1、Windows NT4.0+ORACLE 8.0.4

            2、ORACLE安裝路徑為:C:\ORANT

            含義解釋:

            DECODE(條件,值1,翻譯值1,值2,翻譯值2,...值n,翻譯值n,缺省值)

            該函數(shù)的含義如下:

            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()函數(shù)根據(jù)某個值是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)有一個商品銷售表sale,表結(jié)構(gòu)為:

            month

            char(6)

            --月份

            sell

            number(10,2)

            --月銷售金額

            現(xiàn)有數(shù)據(jù)為:

            200001

            1000

            200002

            1100

            200003

            1200

            200004

            1300

            200005

            1400

            200006

            1500

            200007

            1600

            200101

            1100

            200202

            1200

            200301

            1300

            想要轉(zhuǎn)化為以下結(jié)構(gòu)的數(shù)據(jù):

            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月銷售金額

            結(jié)構(gòu)轉(zhuǎn)化的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);
          posted on 2008-04-23 13:18 HUIKK 閱讀(5176) 評論(0)  編輯  收藏 所屬分類: DataBase
          主站蜘蛛池模板: 马公市| 平遥县| 绥德县| 仁布县| 罗江县| 祥云县| 阿城市| 麻江县| 巩留县| 宜阳县| 洪洞县| 岢岚县| 沽源县| 长白| 舒城县| 浏阳市| 大埔县| 新密市| 临颍县| 得荣县| 兖州市| 阿荣旗| 明水县| 罗平县| 巴林左旗| 海南省| 宁波市| 宣化县| 贵溪市| 江安县| 民丰县| 夏津县| 伊通| 英超| 济宁市| 祁东县| 两当县| 洛浦县| 陕西省| 澄江县| 安顺市|