About
The addition of all numbers in a set is called a total.
Sum can be use as:
- an aggregate
- or a analytic function.
It can follow the full syntax of the analytic function and in this way create Function - (Moving|Rolling|Running) Calculation
It's a binary function.
Associative Property
An addition has an associative property and groups to the left (i.e., is left-associative). 1 + 2 + 3 is equivalent to (1 + 2 ) + 3
Example in Javascript
console.log(1 + 2 + 3); // 6
console.log((1 + 2) + 3); // 6
console.log(1 + "2" + 3); // 123
console.log((1 + "2") + 3); // 123
Different Methods
An addition can be:
- an integer addition
- an floating-point addition.
- or a string concatenation
The two arithmetic operations work on different types of operands and require different functions.
Sigma
The symbol “sigma” is the Greek capital S and is a shortcut for “the sum of”.
- sum of x: sum{}{}{x}
Analytic / Aggregate
The data come from the sample schema SH and the two next statements retrieve the same result.
Aggregate (Group By)
select distinct CUST_LAST_NAME, sum(AMOUNT_SOLD)
from
SH.CUSTOMERS CUST,
SH.SALES SALES
where ( CUST.CUST_ID = SALES.CUST_ID ) AND
( CUST_LAST_NAME like 'Aa%' or CUST_LAST_NAME like 'Ab%' or CUST_LAST_NAME like 'Ac%' )
group by CUST_LAST_NAME
order by CUST_LAST_NAME asc
Analytic / Window (Over )
select distinct CUST_LAST_NAME, sum(AMOUNT_SOLD) over (order by CUST_LAST_NAME)
from
SH.CUSTOMERS CUST,
SH.SALES SALES
where ( CUST.CUST_ID = SALES.CUST_ID ) AND
( CUST_LAST_NAME like 'Aa%' or CUST_LAST_NAME like 'Ab%' or CUST_LAST_NAME like 'Ac%' )
order by CUST_LAST_NAME asc
Analytic and Aggregate Statement
select
distinct CUST_LAST_NAME,
sum(AMOUNT_SOLD) ,
sum(sum(AMOUNT_SOLD) over () -- Grand Total. You must give the two times the SUM function. Otherwise you get a ORA-00979.
from
SH.CUSTOMERS CUST,
SH.SALES SALES
where ( CUST.CUST_ID = SALES.CUST_ID ) AND
( CUST_LAST_NAME like 'Aa%' or CUST_LAST_NAME like 'Ab%' or CUST_LAST_NAME like 'Ac%' )
group by CUST_LAST_NAME
order by CUST_LAST_NAME asc
Identity
The identity function for addition (+) is <MATH> x+0 = x </MATH>