Gyanendra Kumar - 7 days ago 4x

SQL Question

A column has some negative values and some positive values. It is required to find the sum of negative numbers and the sum of the positive numbers in two separate columns.

Thanks in advance.

Answer

Just use `case`

:

```
select sum(case when val > 0 then val end) as pos_sum,
sum(case when val < 0 then val end) as neg_sum
from t;
```

Source (Stackoverflow)

Comments