SQL-列中的值累加 [英] SQL- Add up Values in a Column

查看:1260
本文介绍了SQL-列中的值累加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在SQL列中添加值?我在xampp中设置了表格,并尝试将所有值加到标题为总值"的列中.

How can I add up values in a SQL column? I have the table set up in xampp and I'm trying to add up all the values in a column titled "gross".

推荐答案

SQL Server或MySQL:

select sum(MyColumn) as MyColumnSum from MyTable

如果您需要通过对另一列进行分组来对一列进行求和

select sum(MyColumn) as MyColumnSum, OtherColumn from MyTable Group By OtherColumn

这是一种分别添加负数或正数的方法

select
  sum( case when MyColumn < 0 then MyColumn else 0 end ) as NegativeSum,
  sum( case when MyColumn > 0 then MyColumn else 0 end ) as PositiveSum
from
  MyTable

参考

这篇关于SQL-列中的值累加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆