📅  最后修改于: 2020-11-13 00:52:43             🧑  作者: Mango
SQLite SUM函数用于返回表达式的求和值。
句法:
SELECT SUM(aggregate_expression)
FROM tables
[WHERE conditions];
将SUM函数与GROUP BY子句一起使用时的语法:
SELECT expression1, expression2, ... expression_n
SUM(aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n;
范例1:
我们有一个名为“ STUDENT”的表,其中包含以下数据:
检索ID小于或等于5的学生的总费用:
SELECT SUM(FEES) AS "Total Fees"
FROM STUDENT
WHERE ID <= 5;
输出:
示例2:将数学公式与SUM函数
您可以将数学公式与SUM函数。
SELECT SUM(FEES / 12) AS "Total Monthly Fees"
FROM STUDENT;
输出:
示例2:将GROUP BY子句与SUM函数:
从“学生”表中检索地址,找到其相应费用的总和,然后按地址将结果分组。
SELECT ADDRESS, SUM(FEES) AS "Total Salary"
FROM STUDENT
WHERE ID <= 5
GROUP BY ADDRESS;
输出: