欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

什么是over分析函數(shù)

什么是over分析函數(shù),針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),汕尾企業(yè)網(wǎng)站建設(shè),汕尾品牌網(wǎng)站建設(shè),網(wǎng)站定制,汕尾網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,汕尾網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

over(Partition by...) 一個(gè)超級(jí)牛皮的ORACLE特有函數(shù)。

天天都用ORACLE,用了快2年了。最近才接觸到這個(gè)功能強(qiáng)大而靈活的函數(shù)。真實(shí)慚愧??!

oracle的分析函數(shù)over 及開(kāi)窗函數(shù)
一:分析函數(shù)over
Oracle從8.1.6開(kāi)始提供分析函數(shù),分析函數(shù)用于計(jì)算基于組的某種聚合值,它和聚合函數(shù)的不同之處是
對(duì)于每個(gè)組返回多行,而聚合函數(shù)對(duì)于每個(gè)組只返回一行。 
下面通過(guò)幾個(gè)例子來(lái)說(shuō)明其應(yīng)用。                                       
1:統(tǒng)計(jì)某商店的營(yíng)業(yè)額。        
     date       sale
     1           20
     2           15
     3           14
     4           18
     5           30
    規(guī)則:按天統(tǒng)計(jì):每天都統(tǒng)計(jì)前面幾天的總額
    得到的結(jié)果:
    DATE   SALE       SUM
    ----- -------- ------
    1      20        20           --1天           
    2      15        35           --1天+2天           
    3      14        49           --1天+2天+3天           
    4      18        67            .          
    5      30        97            .
     
2:統(tǒng)計(jì)各班成績(jī)第一名的同學(xué)信息
    NAME   CLASS S                         
    ----- ----- ---------------------- 
    fda    1      80                     
    ffd    1      78                     
    dss    1      95                     
    cfe    2      74                     
    gds    2      92                     
    gf     3      99                     
    ddd    3      99                     
    adf    3      45                     
    asdf   3      55                     
    3dd    3      78              
   
    通過(guò):   
    --
    select * from                                                                       
    (                                                                            
    select name,class,s,rank()over(partition by class order by s desc) mm from t2
    )                                                                            
    where mm=1 
    --
    得到結(jié)果:
    NAME   CLASS S                       MM                                                                                        
    ----- ----- ---------------------- ---------------------- 
    dss    1      95                      1                      
    gds    2      92                      1                      
    gf     3      99                      1                      
    ddd    3      99                      1          
   
    注意:
    1.在求第一名成績(jī)的時(shí)候,不能用row_number(),因?yàn)槿绻嘤袃蓚€(gè)并列第一,row_number()只返回一個(gè)結(jié)果         
    2.rank()和dense_rank()的區(qū)別是:
      --rank()是跳躍排序,有兩個(gè)第二名時(shí)接下來(lái)就是第四名
      --dense_rank()l是連續(xù)排序,有兩個(gè)第二名時(shí)仍然跟著第三名
     
     
3.分類統(tǒng)計(jì) (并顯示信息)
    A   B   C                      
    -- -- ---------------------- 
    m   a   2                      
    n   a   3                      
    m   a   2                      
    n   b   2                      
    n   b   1                      
    x   b   3                      
    x   b   2                      
    x   b   4                      
    h   b   3 
   select a,c,sum(c)over(partition by a) from t2                
   得到結(jié)果:
   A   B   C        SUM(C)OVER(PARTITIONBYA)      
   -- -- ------- ------------------------ 
   h   b   3        3                        
   m   a   2        4                        
   m   a   2        4                        
   n   a   3        6                        
   n   b   2        6                        
   n   b   1        6                        
   x   b   3        9                        
   x   b   2        9                        
   x   b   4        9                        
  
   如果用sum,group by 則只能得到
   A   SUM(C)                            
   -- ---------------------- 
   h   3                      
   m   4                      
   n   6                      
   x   9                      
   無(wú)法得到B列值       
  
=====
select * from test

數(shù)據(jù):
A B C 
1 1 1 
1 2 2 
1 3 3 
2 2 5 
3 4 6

---將B欄位值相同的對(duì)應(yīng)的C 欄位值加總
select a,b,c, SUM(C) OVER (PARTITION BY B) C_Sum
from test

A B C C_SUM 
1 1 1 1 
1 2 2 7 
2 2 5 7 
1 3 3 3 
3 4 6 6

---如果不需要已某個(gè)欄位的值分割,那就要用 null

eg: 就是將C的欄位值summary 放在每行后面

select a,b,c, SUM(C) OVER (PARTITION BY null) C_Sum
from test

A B C C_SUM 
1 1 1 17 
1 2 2 17 
1 3 3 17 
2 2 5 17 
3 4 6 17

求個(gè)人工資占部門工資的百分比

SQL> select * from salary;

NAME DEPT SAL
---------- ---- -----
a 10 2000
b 10 3000
c 10 5000
d 20 4000

SQL> select name,dept,sal,sal*100/sum(sal) over(partition by dept) percent from salary;

NAME DEPT SAL PERCENT
---------- ---- ----- ----------
a 10 2000 20
b 10 3000 30
c 10 5000 50
d 20 4000 100

二:開(kāi)窗函數(shù)           
      開(kāi)窗函數(shù)指定了分析函數(shù)工作的數(shù)據(jù)窗口大小,這個(gè)數(shù)據(jù)窗口大小可能會(huì)隨著行的變化而變化,舉例如下: 
1:     
   over(order by salary) 按照salary排序進(jìn)行累計(jì),order by是個(gè)默認(rèn)的開(kāi)窗函數(shù)
   over(partition by deptno)按照部門分區(qū)
2:
  over(order by salary range between 5 preceding and 5 following)
   每行對(duì)應(yīng)的數(shù)據(jù)窗口是之前行幅度值不超過(guò)5,之后行幅度值不超過(guò)5
   例如:對(duì)于以下列
     aa
     1
     2
     2
     2
     3
     4
     5
     6
     7
     9
   
   sum(aa)over(order by aa range between 2 preceding and 2 following)
   得出的結(jié)果是
            AA                       SUM
            ---------------------- ------------------------------------------------------- 
            1                       10                                                      
            2                       14                                                      
            2                       14                                                      
            2                       14                                                      
            3                       18                                                      
            4                       18                                                      
            5                       22                                                      
            6                       18                                                                
            7                       22                                                                
            9                       9                                                                 
             
   就是說(shuō),對(duì)于aa=5的一行 ,sum為   5-1<=aa<=5+2 的和
   對(duì)于aa=2來(lái)說(shuō) ,sum=1+2+2+2+3+4=14     ;
   又如 對(duì)于aa=9 ,9-1<=aa<=9+2 只有9一個(gè)數(shù),所以sum=9    ;
              
3:其它:
     over(order by salary rows between 2 preceding and 4 following)
          每行對(duì)應(yīng)的數(shù)據(jù)窗口是之前2行,之后4行 
4:下面三條語(yǔ)句等效:           
     over(order by salary rows between unbounded preceding and unbounded following)
          每行對(duì)應(yīng)的數(shù)據(jù)窗口是從第一行到最后一行,等效:
     over(order by salary range between unbounded preceding and unbounded following)
           等效
     over(partition by null)

常用的分析函數(shù)如下所列:

row_number() over(partition by ... order by ...)
rank() over(partition by ... order by ...)
dense_rank() over(partition by ... order by ...)
count() over(partition by ... order by ...)
max() over(partition by ... order by ...)
min() over(partition by ... order by ...)
sum() over(partition by ... order by ...)
avg() over(partition by ... order by ...)
first_value() over(partition by ... order by ...)
last_value() over(partition by ... order by ...)
lag() over(partition by ... order by ...)
lead() over(partition by ... order by ...)

示例
SQL> select type,qty from test;

TYPE QTY
---------- ----------
1 6
2 9

 SQL> select type,qty,to_char(row_number() over(partition by type order by qty))||'/'||to_char(count(*) over(partition by type)) as cnt2 from test;

TYPE QTY CNT2 
---------- ---------- ------------
3 1/2
1 6 2/2
2 5 1/3
7 2/3 
2 9 3/3

 SQL> select * from test;
---------- -------------------------------------------------
1 11111
2 22222
3 33333
4 44444

SQL> select t.id,mc,to_char(b.rn)||'/'||t.id)e
2 from test t,
 (select rownum rn from (select max(to_number(id)) mid from test) connect by rownum <=mid ))L
4 where b.rn<=to_number(t.id)
order by id

ID MC TO_CHAR(B.RN)||'/'||T.ID
--------- -------------------------------------------------- ---------------------------------------------------
1 11111 1/1
2 22222 1/2
2 22222 2/2
3 33333 1/3
3 33333 2/3
3 33333 3/3
 44444 1/4 44444 2/4
4 44444 3/4CNOUG4 44444 4/4

10 rows selected

*******************************************************************

關(guān)于partition by

這些都是分析函數(shù),好像是8.0以后才有的 row_number()和rownum差不多,功能更強(qiáng)一點(diǎn)(可以在各個(gè)分組內(nèi)從1開(kāi)時(shí)排序) rank()是跳躍排序,有兩個(gè)第二名時(shí)接下來(lái)就是第四名(同樣是在各個(gè)分組內(nèi)) dense_rank()l是連續(xù)排序,有兩個(gè)第二名時(shí)仍然跟著第三名。相比之下row_number是沒(méi)有重復(fù)值的 lag(arg1,arg2,arg3): arg1是從其他行返回的表達(dá)式 arg2是希望檢索的當(dāng)前行分區(qū)的偏移量。是一個(gè)正的偏移量,時(shí)一個(gè)往回檢索以前的行的數(shù)目。 arg3是在arg2表示的數(shù)目超出了分組的范圍時(shí)返回的值。

1.
select deptno,row_number() over(partition by deptno order by sal) from emp order by deptno;
2.
select deptno,rank() over (partition by deptno order by sal) from emp order by deptno;
3.
select deptno,dense_rank() over(partition by deptno order by sal) from emp order by deptno;
4.
select deptno,ename,sal,lag(ename,1,null) over(partition by deptno order by ename) from emp ord er by deptno;
5.
select deptno,ename,sal,lag(ename,2,'example') over(partition by deptno order by ename) from em p
order by deptno;
6.
select deptno, sal,sum(sal) over(partition by deptno) from emp;--每行記錄后都有總計(jì)值  select deptno, sum(sal) from emp group by deptno;
7. 求每個(gè)部門的平均工資以及每個(gè)人與所在部門的工資差額

select deptno,ename,sal ,
     round(avg(sal) over(partition by deptno)) as dept_avg_sal, 
     round(sal-avg(sal) over(partition by deptno)) as dept_sal_diff
from emp;

關(guān)于什么是over分析函數(shù)問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

當(dāng)前名稱:什么是over分析函數(shù)
本文鏈接:http://aaarwkj.com/article12/jpocdc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、App設(shè)計(jì)、建站公司網(wǎng)頁(yè)設(shè)計(jì)公司、微信公眾號(hào)自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名
国产女主播精品视频一区| 久久香蕉精品国产亚洲av| 国产黄片一区二区不卡| 国产精品一区二区三区播放| 欧美日韩一区二区综合性色| 亚洲一区二区精品天堂| 加勒比av免费在线播放| 日韩精品人妻一区二区三区免费| 四虎海外免费永久地址| 成人午夜激情在线观看| 久久精品一区二区婷婷| 国产精品_国产精品_k频道| 亚洲国产欧美日韩激情在线| 91香蕉国产精品日韩| 内射嫩国产欧美国产日韩欧美| 日本熟妇一区二区三区在线视频| 年轻的母亲韩国三级| 国产三级精品正在播放| 久久精品国产精品亚洲片| 国产一区av麻豆免费观看| 深夜成人免费观看视频| 久久综合久久狠狠激情| 99热这里只有精品56| 无遮挡动漫网站免费观看| 一区二区在线视频国产| 风韵丰满熟妇啪啪老熟女| 中文字幕亚洲精品99| 高清美女视频亚洲免费| 一个人看的视频天堂色| 中文字幕乱码日韩一二三区| 欧美中文日韩国产字幕| 91麻豆国产福利在线| 精品久久久久久亚洲电影| 国产日韩欧美老年人激情| 99人妻一区二区三区在线| 天天爽天天看天天射天天操| 亚洲欧美日韩精品麻豆| 国产精品一区二区久久蜜桃麻豆| 成人永久免费播放平台| 国产日韩综合精品一区| 日韩专区欧美二区国产|