SUM Function
Description
The SUM function calculates and returns the total sum of a set of numeric data. If the DISTINCT keyword is specified, the sum of the distinct set of values is calculated.
Parameter Description
expr: The numeric type field to be summed, which can be of typeTINYINT,SMALLINT,INT,BIGINT,FLOAT,DOUBLE, orDECIMAL.
Return Type
- For
DECIMALtype data, aDECIMALtype result is returned. In other cases, aDOUBLEtype result is returned. - When the
DISTINCTkeyword is specified, the sum of the distinct set of values is returned. - If the expression contains
NULLvalues, theNULLvalues will not participate in the calculation.
Usage Examples
- Calculate the total sum of all values:
- Calculate the sum of the deduplicated set of values:
- Use the FILTER clause to conditionally calculate the sum:
- Combine the FILTER clause and DISTINCT to calculate the deduplicated conditional sum:
With the above examples, you can better understand the usage and application scenarios of the SUM function. Please adjust the parameters and expressions according to your actual needs.
