I was getting a divide by 0 error with this code:
CASE
WHEN DENOMINATOR >= 0
THEN SUM(INT1 * INT2 / DENOMINATOR)
ELSE 0
END AS RATIO
However when I changed to the following code, it worked.
CASE
WHEN DENOMINATOR >= 0
THEN SUM(INT1) * INT2 / DENOMINATOR
ELSE 0
END AS RATIO
Could someone help me understand the reason so I can avoid this in the future? BTW, the first sample worked in Vertica. I realize summing just what needs to be summed rather than doing the calculation before the summation is a better programming practice. However still am curious.
CASE WHEN DENOMINATOR >= 0
.... seems wrong... if you're trying to avoid a divide by 0 error... don't you mean>
0 not>=
or just<>
0 if you want to allow negatives? – Marquis