开发者

using SUM and presenting result as absolute (ABS)

I have both positive and negative numbers (money) in a column and need to:

  1. SUM the total ie. SUM(myColumn) based on if the numbers are +/-
  2. Present the result as an absolute ie. even though the result is -1234 it should be presented as 1234

SQL is not my tr开发者_StackOverflow中文版ade as you probably notice but we've solved most other issues but this one so any help is appriciated. Keep in mind my skill level is very low


You will have to use a combination of the sum and abs aggregate functions in SQL. Since you want the absolute value of the sum, the sum function will need to be called inside the call to abs:

select abs(sum(columnName)) from table

That should work for both SQL Server, MySQL, and Oracle.


Try one (or more) of these

SELECT SUM(moneyColumn) FROM MyTable
SELECT SUM(ABS(moneyColumn) FROM MyTable
SELECT ABS(SUM(moneyColumn) FROM MyTable
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜