sum over用法,以及與group by的區別

來源:互聯網
上載者:User

1、sum over用法

sum(col1) over(partition by col2 order by col3 )

以上的函數可以理解為:按col2 進行分組(partition ),每組以col3 進行排序(order),並進行連續加總(sum)

表a,內容如下:  
 B   C  D  
02 02 1  
02 03 2  
02 04 3  
02 05 4  
02 01 5  
02 06 6  
02 07 7  
02 03 5  
02 02 12  
02 01 2  
02 01 23  

執行:SELECT   b,   c,   d,   SUM(d)   OVER(PARTITION   BY   b,c   ORDER   BY   d)   e   FROM   a  

得到結果:

B   C   E  
02 01 2  
02 01 7  
02 01 30  
02 02 1  
02 02 13  
02 03 2  
02 03 7  
02 04 3  
02 05 4  
02 06 6  
02 07 7  

可以看到,E列的值是累加和。


2、與group by 的區別

同樣的a表:select   b,c,sum(d)   e   from   a   group   by   b,c  

B   C   E  
02 01 30  
02 02 13  
02 03 7  
02 04 3  
02 05 4  
02 06 6  
02 07 7

區別很明顯。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.