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

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

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

    【永恒的瞬間】
    ?Give me hapy ?
    oracle分析函數(shù)

    zhouwf0726 | 25 七月, 2006 12:51

    oracle分析函數(shù)--SQL*PLUS環(huán)境
    --1、GROUP BY子句

    --CREATE TEST TABLE AND INSERT TEST DATA.
    create table students
    (id number(15,0),
    area varchar2(10),
    stu_type varchar2(2),
    score number(20,2));

    insert into students values(1, '111', 'g', 80 );
    insert into students values(1, '111', 'j', 80 );
    insert into students values(1, '222', 'g', 89 );
    insert into students values(1, '222', 'g', 68 );
    insert into students values(2, '111', 'g', 80 );
    insert into students values(2, '111', 'j', 70 );
    insert into students values(2, '222', 'g', 60 );
    insert into students values(2, '222', 'j', 65 );
    insert into students values(3, '111', 'g', 75 );
    insert into students values(3, '111', 'j', 58 );
    insert into students values(3, '222', 'g', 58 );
    insert into students values(3, '222', 'j', 90 );
    insert into students values(4, '111', 'g', 89 );
    insert into students values(4, '111', 'j', 90 );
    insert into students values(4, '222', 'g', 90 );
    insert into students values(4, '222', 'j', 89 );
    commit;

    col score format 999999999999.99

    --A、GROUPING SETS

    select id,area,stu_type,sum(score) score
    from students
    group by grouping sets((id,area,stu_type),(id,area),id)
    order by id,area,stu_type;

    /*--------理解grouping sets
    select a, b, c, sum( d ) from t
    group by grouping sets ( a, b, c )

    等效于

    select * from (
    select a, null, null, sum( d ) from t group by a
    union all
    select null, b, null, sum( d ) from t group by b
    union all
    select null, null, c, sum( d ) from t group by c
    )
    */

    --B、ROLLUP

    select id,area,stu_type,sum(score) score
    from students
    group by rollup(id,area,stu_type)
    order by id,area,stu_type;

    /*--------理解rollup
    select a, b, c, sum( d )
    from t
    group by rollup(a, b, c);

    等效于

    select * from (
    select a, b, c, sum( d ) from t group by a, b, c
    union all
    select a, b, null, sum( d ) from t group by a, b
    union all
    select a, null, null, sum( d ) from t group by a
    union all
    select null, null, null, sum( d ) from t
    )
    */

    --C、CUBE

    select id,area,stu_type,sum(score) score
    from students
    group by cube(id,area,stu_type)
    order by id,area,stu_type;

    /*--------理解cube
    select a, b, c, sum( d ) from t
    group by cube( a, b, c)

    等效于

    select a, b, c, sum( d ) from t
    group by grouping sets(
    ( a, b, c ),
    ( a, b ), ( a ), ( b, c ),
    ( b ), ( a, c ), ( c ),
    () )
    */

    --D、GROUPING

    /*從上面的結(jié)果中我們很容易發(fā)現(xiàn),每個(gè)統(tǒng)計(jì)數(shù)據(jù)所對(duì)應(yīng)的行都會(huì)出現(xiàn)null,
    如何來(lái)區(qū)分到底是根據(jù)那個(gè)字段做的匯總呢,grouping函數(shù)判斷是否合計(jì)列!*/

    select decode(grouping(id),1,'all id',id) id,
    decode(grouping(area),1,'all area',to_char(area)) area,
    decode(grouping(stu_type),1,'all_stu_type',stu_type) stu_type,
    sum(score) score
    from students
    group by cube(id,area,stu_type)
    order by id,area,stu_type;

    --2、OVER()函數(shù)的使用
    --1、RANK()、DENSE_RANK() 的、ROW_NUMBER()、CUME_DIST()、MAX()、AVG()

    break on id skip 1
    select id,area,score from students order by id,area,score desc;

    select id,rank() over(partition by id order by score desc) rk,score from students;

    --允許并列名次、名次不間斷
    select id,dense_rank() over(partition by id order by score desc) rk,score from students;

    --即使SCORE相同,ROW_NUMBER()結(jié)果也是不同
    select id,row_number() over(partition by ID order by SCORE desc) rn,score from students;

    select cume_dist() over(order by id) a, --該組最大row_number/所有記錄row_number
    row_number() over (order by id) rn,id,area,score from students;

    select id,max(score) over(partition by id order by score desc) as mx,score from students;

    select id,area,avg(score) over(partition by id order by area) as avg,score from students; --注意有無(wú)order by的區(qū)別

    --按照ID求AVG
    select id,avg(score) over(partition by id order by score desc rows between unbounded preceding
    and unbounded following ) as ag,score from students;


    --2、SUM()

    select id,area,score from students order by id,area,score desc;

    select id,area,score,
    sum(score) over (order by id,area) 連續(xù)求和, --按照OVER后邊內(nèi)容匯總求和
    sum(score) over () 總和, -- 此處sum(score) over () 等同于sum(score)
    100*round(score/sum(score) over (),4) "份額(%)"
    from students;

    select id,area,score,
    sum(score) over (partition by id order by area ) 連id續(xù)求和, --按照id內(nèi)容匯總求和
    sum(score) over (partition by id) id總和, --各id的分?jǐn)?shù)總和
    100*round(score/sum(score) over (partition by id),4) "id份額(%)",
    sum(score) over () 總和, -- 此處sum(score) over () 等同于sum(score)
    100*round(score/sum(score) over (),4) "份額(%)"
    from students;

    --4、LAG(COL,n,default)、LEAD(OL,n,default) --取前后邊N條數(shù)據(jù)

    select id,lag(score,1,0) over(order by id) lg,score from students;

    select id,lead(score,1,0) over(order by id) lg,score from students;

    --5、FIRST_VALUE()、LAST_VALUE()

    select id,first_value(score) over(order by id) fv,score from students;

    select id,last_value(score) over(order by id) fv,score from students;

    posted on 2007-04-25 20:46 ???MengChuChen 閱讀(526) 評(píng)論(1)  編輯  收藏 所屬分類: ORACLE

    FeedBack:
    # re: oracle分析函數(shù)
    2008-12-18 10:27 | L_yongfei
    收藏。。。  回復(fù)  更多評(píng)論
      
    主站蜘蛛池模板: 在线A亚洲老鸭窝天堂| 亚洲AV午夜福利精品一区二区| 免费无码一区二区| 国产亚洲精品岁国产微拍精品| 一区二区免费视频| 亚洲精品av无码喷奶水糖心| 精品久久久久久亚洲| 成人免费在线看片| 边摸边脱吃奶边高潮视频免费| 亚洲一二成人精品区| 国产视频精品免费| 久久免费国产精品一区二区| 国产婷婷综合丁香亚洲欧洲| 中文字幕第一页亚洲| 在线a毛片免费视频观看| 中文字幕乱码系列免费| 国产亚洲精品影视在线| 人人狠狠综合久久亚洲88| 国产美女无遮挡免费视频| 无人在线观看免费高清| 人妖系列免费网站观看| 亚洲一区电影在线观看| 国产亚洲精品观看91在线| 日韩在线视频免费看| 日韩在线永久免费播放| 农村寡妇一级毛片免费看视频| 亚洲免费福利在线视频| 亚洲日本中文字幕| 亚洲一级Av无码毛片久久精品| 国产香蕉九九久久精品免费| 女人体1963午夜免费视频| 边摸边吃奶边做爽免费视频网站| 亚洲六月丁香六月婷婷蜜芽| 亚洲成色在线影院| 亚洲高清无码在线观看| 成人爱做日本视频免费| 999国内精品永久免费视频| 香港a毛片免费观看 | 亚洲精品免费在线观看| 美女被吸屁股免费网站| 亚洲日本在线电影|