Home >Database >Mysql Tutorial >sumover用法,以及与groupby的区别

sumover用法,以及与groupby的区别

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 15:58:182556browse

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

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

区别很明显。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn