SQL SUM函数

SQL SUM()函数用于得到一列或一表达式的总和。

SQL SUM()函数语法

SELECT SUM(列名)
FROM 数据库表名
WHERE 条件

SQL SUM() 函数范例

数据表: Employees

EmployeeId FirstName LastName Department Salary
203 Yilfaee Uyfda Finance 88000
204 Bklda Ylafaea Finance 51800
205 Qfajfda Nhjda Finance 47000
206 Opad Xjkdl Finance 62300
302 Hgjfdkl Bkldfa Development 95000
303 Uiofda Whfdjks Development 35000
304 Pids Vcds Development 41000

例 1
我们想知道所有职员的工资总和:

SELECT SUM(Salary) AS TotalSalary
FROM Employees

结果会类似于:

TotalSalary
420100

例2,使用GROUP BY
我们想知道每个部门工资的总和:

SELECT Department, SUM(Salary) AS TotalSalary
FROM Employees
GROUP BY Department

结果会类似于:

Department TotalSalary
Finance 249100
Development 171000