hrishi - 10 months ago 74

SQL Question

I am using

`SUM()`

`SUM()`

`SUM()`

`20.00`

20.00

20.00

20.00

-20.00

20.00

20.00

40.00

20.00

20.00

20.00

20.00

20.00

-20.00

-20.00

20.00

sum() should return 220 and not 440.

Is returning 440.

Answer Source

To subtract negative numbers rather than add them you would use `SUM(ABS(col))`

but just to check this is what you actually need example results below.

```
WITH YourTable(col) AS
(
SELECT 2 UNION ALL
SELECT -5
)
SELECT
SUM(ABS(col)) AS [SUM(ABS(col))],
SUM(col) AS [SUM(col)]
FROM YourTable
```

Returns

```
SUM(ABS(col)) SUM(col)
------------- -----------
7 -3
```