Syntax
SUM([ DISTINCT | ALL ] expr) OVER (analytic_clause)
Purpose
SUM() returns the sum of expr. If the query result set does not contain any row, SUM() returns NULL. The DISTINCT keyword can be used to find the sum of distinct values of expr.
If no matching row is found, SUM() returns NULL.
Examples
obclient> CREATE TABLE employees(last_name CHAR(10), salary DECIMAL, job_id CHAR(32));
Query OK, 0 rows affected
obclient> INSERT INTO employees VALUES('JIM', 2000, 'CLEANER');
Query OK, 1 row affected
obclient> INSERT INTO employees VALUES('MIKE', 12000, 'ENGINEERING');
Query OK, 1 row affected
obclient> INSERT INTO employees VALUES('LILY', 13000, 'ENGINEERING');
Query OK, 1 row affected
obclient> INSERT INTO employees VALUES('TOM', 11000, 'ENGINEERING');
Query OK, 1 row affected
obclient> SELECT last_name, SUM(salary) OVER (PARTITION BY job_id) total_s FROM employees;
+-----------+---------+
| last_name | total_s |
+-----------+---------+
| JIM | 2000 |
| MIKE | 36000 |
| LILY | 36000 |
| TOM | 36000 |
+-----------+---------+
4 rows in set