SUM()
与OVER一起使用聚合函数。让我们首先创建一个表-
mysql> create table DemoTable ( CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, CustomerValue int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(CustomerValue) values(10); mysql> insert into DemoTable(CustomerValue) values(20); mysql> insert into DemoTable(CustomerValue) values(30); mysql> insert into DemoTable(CustomerValue) values(40);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+------------+---------------+ | CustomerId | CustomerValue | +------------+---------------+ | 1 | 10 | | 2 | 20 | | 3 | 30 | | 4 | 40 | +------------+---------------+ 4 rows in set (0.00 sec)
以下是以特定方式汇总单个列中的值的查询-
mysql> select CustomerId, SUM(CustomerValue) OVER (ORDER BY CustomerId) SpecificSum from DemoTable;
这将产生以下输出-
+------------+-------------+ | CustomerId | SpecificSum | +------------+-------------+ | 1 | 10 | | 2 | 30 | | 3 | 60 | | 4 | 100 | +------------+-------------+ 4 rows in set (0.00 sec)