如何对SQL查询中的两个字段求和 [英] How to SUM two fields within an SQL query
本文介绍了如何对SQL查询中的两个字段求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要获取同一行中两个字段的总数,然后在同一行末尾的字段中输入该数字.
I need to get the total of two fields which are within the same row and input that number in a field at the end of that same row.
这是我的代码.
Sum(tbl1.fld1 + tbl1.fld2) AS [Total]
这是SUM函数的用途,还是只能使用SUM函数获取列的总数?
Is this what the SUM function is used for, or can you only use the SUM function for getting the total of a column?
谢谢
推荐答案
SUM
是聚合函数.它将计算每个组的总数. +
用于计算一行中的两列或更多列.
SUM
is an aggregate function. It will calculate the total for each group. +
is used for calculating two or more columns in a row.
请考虑以下示例
ID VALUE1 VALUE2
===================
1 1 2
1 2 2
2 3 4
2 4 5
SELECT ID, SUM(VALUE1), SUM(VALUE2)
FROM tableName
GROUP BY ID
将导致
ID, SUM(VALUE1), SUM(VALUE2)
1 3 4
2 7 9
SELECT ID, VALUE1 + VALUE2
FROM TableName
将导致
ID, VALUE1 + VALUE2
1 3
1 4
2 7
2 9
SELECT ID, SUM(VALUE1 + VALUE2)
FROM tableName
GROUP BY ID
将导致
ID, SUM(VALUE1 + VALUE2)
1 7
2 16
这篇关于如何对SQL查询中的两个字段求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文