Subject adding positive and negative values in differents columns
Author Sergio H. Gonzalez
Hello, having this simple table with two fields,

ID IMPORTE
---------------
1 100
1 -90
1 800
2 -1
2 2

If I want to have a select with 3 cols, like this

ID POSITIVE NEGATIVE
-------------------------
1 900 90
2 2 1

Is this the best way to do it? Or there are some better ways?

SELECT
D.id,
Sum(case when D.importe > 0
then D.importe
else 0 end) as positive,
Sum(case when D.importe < 0
then Abs(D.importe)
else 0 end) as negative
FROM
MyTable D
GROUP BY
D.id
ORDER BY
D.id

Thanks!!

sergio