Content navigation
1. Definitions
SUM function returns a non-null set of digital sum value of the expression
2, Grammar
The syntax is as follows:
SUM( [DISTINCT | ALL] expr )
This syntax can be decomposed as follows:
- SUM (DISTINCT expr)
provides a unique value expr After calculating each row in the group will be returned and obtained by adding. - SUM (ALL expr)
providing group, and the sum of each row and expr and ignores null values. - SUM(expr)
Note :
Data expr type argument must be NUMBER.
3, Code
/*
作者:AT阿宝哥
日期:2016年9月18日
愿景:参考官方资料,做最好的课程,成就更多职业人!
邮箱:[email protected]
CSDN:https://blog.csdn.net/goldentec
简书:https://www.jianshu.com/u/8a6075d7a2e0
说明:
注意:
*/
-------------------------------------------------------------------------------
--Sample1:
SELECT sum(2) FROM emp;
select sum(3) from emp;
SELECT sum(4) FROM emp;
-------------------------------------------------------------------------------
--Sample2:获得组(整个表)中各行SAL列值并相加,忽略空值.
SELECT * FROM emp;
SELECT sum(sal) FROM emp;
SELECT sum(comm) FROM emp;
-------------------------------------------------------------------------------
--Sample3:将列中的唯一值添加到总和之中.
SELECT * FROM emp;
SELECT sum(DISTINCT sal) FROM emp;
SELECT sum(DISTINCT comm) FROM emp;
-------------------------------------------------------------------------------