SQL 计算sql查询中特定列的总数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9308849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate total of particular column in sql query
提问by Stone Cold
I want to calculate total of particular column
我想计算特定列的总数
For ex my table must looks like this
例如,我的桌子必须看起来像这样
Customername Payment id RunningTotal
a 500 5 5
b 500 10 10
c 300 10 7
------ -----------
1300 22
I am getting the table but now I want to calculate the total mentioned at the end for the column Payment and RunningTotal.
我正在获取表格,但现在我想计算最后提到的 Payment 和 RunningTotal 列的总数。
回答by mankuTimma
If you are getting the above result from table t1, then you can add your sum at the end by using an Union statement. Something like this
如果您从表 t1 中得到上述结果,那么您可以使用 Union 语句在最后添加您的总和。像这样的东西
select Customername, Payment, id, RunningTotal
from t1
union all
select null,sum(payment),null,sum(runningtotal or any total)
from t1
This will add total payments and the other total at the end of the result.
这将在结果末尾添加总付款和其他总付款。
回答by kishan verma
SELECT Sum(Payment) AS Total FROM tablename;
Output: Total = 1300
输出:总计 = 1300
回答by juergen d
select sum(Payment) as SumPayment, sum(RunningTotal) as SumRunningTotal
from yourTable
回答by sachin
Without GROUPBY
clause use OVER()
不GROUPBY
使用子句OVER()
Example:select Customername, sum(Payment) OVER () AS TotalPayment, id, sum(RunningTotal) over()
as RunningTotal
from t1
示例:select Customername, sum(Payment) OVER () AS TotalPayment, id, sum(RunningTotal) over()
作为 RunningTotal fromt1
回答by checkorbored
if you want to sum all rows, its as simple as:
如果你想对所有行求和,它很简单:
select sum(payment) payment_sum, sum(runningtotal) runningtotal_sum
from customers;