Anyway to subtract the result of one case statemen...
# suiteanalytics
a
Anyway to subtract the result of one case statement from another? Maybe I am attacking this the wrong way, here is my code
(CASE WHEN TO_CHAR({trandate}, 'YYYY') = (TO_CHAR({today}, 'YYYY')) AND TO_CHAR({shipdate}, 'MM') = 1 AND {type} = 'Sales Order' THEN {amount} Else 0) - (CASE WHEN TO_CHAR({trandate}, 'YYYY') = (TO_CHAR({today}, 'YYYY')) AND TO_CHAR({shipdate}, 'MM') = 1 AND {type} = 'Purchase Order' THEN {amount} Else 0) END
Both work individually on their own, just can't quite figure out how to put them together
b
each case statement should have an end statement at the end
that said, i dont think subtracting them does anything interesting since no row will have a type of both
Sales Order
and
Purchase Order
a
Good call - I was able to reconfigure and get it to work with the following code
CASE WHEN TO_CHAR({trandate}, 'YYYY') = (TO_CHAR({today}, 'YYYY')) AND TO_CHAR({shipdate}, 'MM') = 1 AND {type} = 'Sales Order' THEN {amount}
WHEN TO_CHAR({trandate}, 'YYYY') = (TO_CHAR({today}, 'YYYY')) AND TO_CHAR({shipdate}, 'MM') = 1 AND {type} = 'Purchase Order' THEN {amount}*-1 Else 0 END
Those are things I overlook when trying this on a Sunday night lol
m
You should be able to simplify to
CASE WHEN TO_CHAR({trandate}, 'YYYY') = (TO_CHAR({today}, 'YYYY')) AND TO_CHAR({shipdate}, 'MM') = 1 THEN {amount} * DECODE({type}, 'Sales Order', 1, 'Purchase Order', -1, 0) ELSE 0 END
👍 1
a
Nice, I always forget to use Decode. Thank you!