Skip to main content
Version: V1.0.0

SUM

Declaration

SUM([DISTINCT | ALL] expr)

Description

Returns the total of the expr values. If no rows are in the set, it returns NULL. You can use the DISTINCT keyword to get the total of distinct values of expr.

The function also returns NULL if no rows match the condition.

Examples

SELECT * FROM oceanbasetest;
+------+------+------+
| id | ip | ip2 |
+------+------+------+
| 1 | 4 | NULL |
| 3 | 3 | NULL |
| 4 | 3 | NULL |
+------+------+------+
3 rows in set (0.002 sec)

SELECT SUM(ip2),SUM(ip),SUM(DISTINCT(ip)) FROM oceanbasetest;
+----------+---------+-------------------+
| sum(ip2) | sum(ip) | sum(distinct(ip)) |
+----------+---------+-------------------+
| NULL | 10 | 7 |
+----------+---------+-------------------+
1 row in set (0.001 sec)