so you don't need to multiply by -1 you can just negate the value:
SELECT
order_id,
sum(iff(returned,-(price * quantity), 0)) as returnedAmount
FROM VALUES
(123,456,True,50,1),
(987,123,True,10,2)
t(order_id, item_id, returned, price,quantity)
GROUP BY 1
ORDER BY 1;
gives:
ORDER_ID |
RETURNEDAMOUNT |
123 |
-50 |
987 |
-20 |
So to the null, so ether value could null and as Lukasz showed, you can fix that on the outside of the sum, there are a few options ZEROIFNULL, COALESCE, NVL, IFNULL.
if you want the value zero, I feel zeroifnull is explicit, while the other three you have to parse the expression all the way to the right to see the alternative value.
SELECT
order_id,
sum(iff(returned, -(price * quantity), 0)) as ret_a,
zeroifnull(sum(iff(returned, -(price * quantity), 0))) as ret_b,
coalesce(sum(iff(returned, -(price * quantity), 0)),0) as re_c,
nvl(sum(iff(returned, -(price * quantity), 0)),0) as ret_d,
ifnull(sum(iff(returned, -(price * quantity), 0)),0) as ret_e
FROM VALUES
(123,456,True,50,1),
(987,123,True,10,2),
(988,123,True,null,2),
(989,123,True,10,null),
(989,123,True,null,null)
t(order_id, item_id, returned, price,quantity)
GROUP BY 1
ORDER BY 1;
gives:
ORDER_ID |
RET_A |
RET_B |
RET_C |
RET_D |
RET_E |
123 |
-50 |
-50 |
-50 |
-50 |
-50 |
987 |
-20 |
-20 |
-20 |
-20 |
-20 |
988 |
null |
0 |
0 |
0 |
0 |
989 |
null |
0 |
0 |
0 |
0 |