Gyanendra Kumar Gyanendra Kumar - 1 month ago 14
SQL Question

Find the sum of negative numbers and the sum of the positive numbers

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;
Comments