计算sql查询中特定列的总数

时间:2012-02-16 09:44:00

标签: sql

我想计算特定列的总数

对于ex,我的表必须看起来像这样

     Customername  Payment    id    RunningTotal
       a           500        5          5
       b           500        10         10
       c           300        10         7
                  ------              -----------
                   1300                  22  

我正在获取该表,但现在我想计算最后提到的支付和RunningTotal列的总数。

5 个答案:

答案 0 :(得分:5)

如果从表t1获得上述结果,则可以使用Union语句在末尾添加总和。像这样的东西

select Customername, Payment, id, RunningTotal
from t1
union all
select null,sum(payment),null,sum(runningtotal or any total)
from t1

这将在结果的末尾添加总付款和其他总额。

答案 1 :(得分:3)

SELECT Sum(Payment) AS Total FROM tablename;

输出:总计= 1300

答案 2 :(得分:1)

select sum(Payment) as SumPayment, sum(RunningTotal) as SumRunningTotal
from yourTable

答案 3 :(得分:0)

如果你想对所有行求和,就像它一样简单:

select sum(payment) payment_sum, sum(runningtotal) runningtotal_sum
from customers;

答案 4 :(得分:0)

不使用GROUPBY子句,请使用OVER()

示例:
select Customername, sum(Payment) OVER () AS TotalPayment, id, sum(RunningTotal) over() 作为RunningTotal 来自t1