<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Decode360's Blog

    業(yè)精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
      302 隨筆 :: 26 文章 :: 82 評論 :: 0 Trackbacks

    ??? 前段時間和同事研究了一個小問題,關于彩票的中獎判斷,一個Java的題,不過發(fā)現(xiàn)用SQL寫要更加簡單一點,就寫了一個練練手。題目是這樣的:

    ??? 在數(shù)據(jù)庫中存放各個客戶ID,及所選號碼,每個用戶的號碼可能會不止一組。中獎規(guī)則如下:每個位置可選數(shù)字0-9,共7位。連續(xù)兩位數(shù)字及位置均相同時為5等獎,連續(xù)3位數(shù)字及位置相同為4等獎,……,全部數(shù)字及位置相同為特等獎。最后返回所有中獎者名單及中獎數(shù)量。

    ?

    ??? 主要是因為位置是固定的,所以比較方便,用decode將每一位拆分出來與答案比較即可得出結論:

    ?

    create table t9 ( id varchar2 ( 6 ),num varchar2 ( 7 ));

    insert into t9 values ( '10001' , '7483627' );

    insert into t9 values ( '10002' , '9623584' );

    insert into t9 values ( '10004' , '1234569' );

    insert into t9 values ( '10002' , '3298648' );

    insert into t9 values ( '10002' , '5629874' );

    insert into t9 values ( '10003' , '0245896' );

    insert into t9 values ( '10001' , '1259567' );

    commit ;

    ?

    ?

    select id ,k|| '*' || sum (decode(k, '0' , 0 , 1 )) x from

    ( select id ,num,rn,

    ???????? ( case when rn like '%1111111%' then ' 特等獎 '

    ?????????????? when rn like '%111111%' then ' 一等獎 '

    ?????????????? when rn like '%11111%' then ' 二等獎 '

    ?????????????? when rn like '%1111%' then ' 三等獎 '

    ?????????????? when rn like '%111%' then ' 四等獎 '

    ?????????????? when rn like '%11%' then ' 五等獎 '

    ?????????????? else '0'

    ?????????????? end ) k from

    ???? ( select id ,num,a||b||c||d||e||f||g rn from

    ??????? ( select id ,num,

    ???????? decode(a.a,w.a, 1 , 0 ) a,

    ???????? decode(a.b,w.b, 1 , 0 ) b,

    ???????? decode(a.c,w.c, 1 , 0 ) c,

    ???????? decode(a.d,w.d, 1 , 0 ) d,

    ???????? decode(a.e,w.e, 1 , 0 ) e,

    ???????? decode(a.f,w.f, 1 , 0 ) f,

    ???????? decode(a.g,w.g, 1 , 0 ) g? from

    ?????????? ( select id ,num,substr(num, 1 , 1 ) a,substr(num, 2 , 1 ) b,substr(num, 3 , 1 ) c,

    ??????????? substr(num, 4 , 1 ) d,substr(num, 5 , 1 ) e,substr(num, 6 , 1 ) f,substr(num, 7 , 1 ) g

    ??????????? from t9) a,

    ?????????? ( select & 1 a,& 2 b,& 3 c,& 4 d,& 5 e,& 6 f,& 7 g from dual) w --輸入 每期中將號碼

    ?????? )

    ???? )

    )

    group by id ,k

    having sum (decode(k, '0' , 0 , 1 ))<> 0--不顯示未中獎者名單

    ;

    ?

    ?

    ?

    ?

    ?

    ??? 另外涉及到這個題目的一些衍生問題,如 每個ID只生成一條記錄,所有號用“,”分開,如何轉換為T9中的格式:

    ???

    ??? 由于每一組號碼的長度是固定的(7位),所以可以根據(jù)字段長度來半段出某個ID購買了幾注——(length(num)+1)/8

    ??? 這樣可以使用一下代碼將每一注號碼區(qū)分出來:

    ?

    create table t8 ( id varchar2 ( 6 ),num varchar2 ( 999 ));

    insert into t8 values ( '10001' , '7483627,5693268,1259026,0326597,1265975,1236498,0031699,1258962,1359871,1326987' );

    insert into t8 values ( '10002' , '9623584,1236895,2156298,2356897,2369654,1259864,2145236' );

    insert into t8 values ( '10003' , '1234569,1236598,1254785,1254696,1268954' );

    insert into t8 values ( '10004' , '3298648' );

    commit ;

    ?

    select distinct a.id,substr(a.num,instr(a.num, ',' , 1 ,b.rn)+ 1 , 7 ) num from

    ( select id ,num from t8) a,

    ( select rownum rn from dual connect by rownum <= 100 ) b

    order by 1 , 2

    ?

    ??? 通過instr函數(shù)找到“,”的位置,然后通過substr找到號碼。如果號碼的位數(shù)不確定,也可以通過

    ??? instr(num,',',1,rn+1)-instr(num,',',1,rn)

    ??? 來計算位數(shù),從而得到結果。

    ?

    ??? 這里直接使用rownum<=100,限制了每個ID的數(shù)量,而且直接使用了hash join,會造成系統(tǒng)效率比較低,暫時沒有考慮優(yōu)化的問題。

    ?

    ?

    ?

    ???

    ??? 關于一般的彩票中存在的數(shù)字不重復,且無需按位數(shù)排列,或者數(shù)字可選兩位數(shù)等等,寫PLSQL要比直接寫SQL代碼簡單得多,以后有空再整理一下。

    ?

    ?

    ?

    ?





    -The End-

    posted on 2008-09-01 14:35 decode360-3 閱讀(188) 評論(0)  編輯  收藏 所屬分類: SQL Dev
    主站蜘蛛池模板: 亚洲一级特黄大片无码毛片| 黄网址在线永久免费观看 | 亚洲人成图片小说网站| 色偷偷尼玛图亚洲综合| 青青青国产免费一夜七次郎 | 好爽好紧好大的免费视频国产 | 在线精品亚洲一区二区小说| 中文在线免费不卡视频| 国产亚洲视频在线播放| 中文无码成人免费视频在线观看| 亚洲精品狼友在线播放| 中文字幕在线视频免费观看| 亚洲熟妇av一区二区三区漫画| 免费看一区二区三区四区| 亚洲AV成人精品网站在线播放| 免费国产成人α片| 久久精品国产亚洲77777| 99久久精品日本一区二区免费| 亚洲日本国产综合高清| 日韩成人免费在线| 一本久久A久久免费精品不卡| 亚洲国产高清视频| 男女免费观看在线爽爽爽视频| 亚洲成a∨人片在无码2023 | 91短视频在线免费观看| 亚洲videosbestsex日本| 日韩一级免费视频| 中国性猛交xxxxx免费看| 91在线精品亚洲一区二区| 成年女人免费v片| 产传媒61国产免费| 久久久久亚洲Av片无码v| 久久精品女人天堂AV免费观看| 羞羞漫画小舞被黄漫免费| 婷婷亚洲久悠悠色悠在线播放| 国产精品爱啪在线线免费观看| 免费一级特黄特色大片| 亚洲午夜精品一区二区| 国产成人啪精品视频免费网| 免费人妻无码不卡中文字幕系| 亚洲午夜成人精品无码色欲|