如何在 SQL Server 2005 中查找表中多列的总和? [英] How to find sum of multiple columns in a table in SQL Server 2005?
本文介绍了如何在 SQL Server 2005 中查找表中多列的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表 Emp
有这些行:
I have a table Emp
which has these rows:
Emp_cd | Val1 | Val2 | Val3 | Total
-------+-------+-------+-------+-------
1 | 1.23 | 2.23 | 3.43 |
2 | 23.03 | 12.23 | 2.92 |
3 | 7.23 | 9.05 | 13.43 |
4 | 03.21 | 78.23 | 9.43 |
我想找到 Val1、Val2、Val3
的 SUM
并将其显示在 Total
列中.
I want to find SUM
of Val1, Val2, Val3
and which will show in the Total
column.
推荐答案
简单:
SELECT
Val1,
Val2,
Val3,
(Val1 + Val2 + Val3) as 'Total'
FROM Emp
或者如果您只想要一行:
or if you just want one row:
SELECT
SUM(Val1) as 'Val1',
SUM(Val2) as 'Val2',
SUM(Val3) as 'Val3',
(SUM(Val1) + SUM(Val2) + SUM(Val3)) as 'Total'
FROM Emp
这篇关于如何在 SQL Server 2005 中查找表中多列的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文