Gyanendra Kumar - 8 months ago 58

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)