I am having problems with dividing by zero. If the denominator is zero I would like the value to be zero. When I try using nullif, I end up with a zero or one for the calculated value.
Here is the SQL:
Select
StateCode, Month1Date,(Sum(Order)/Sum(Value)) as myValue
from tblOrders
inner join tblStates on OrderStateCode = StateCode
group by StateCode, Month1Date
A
0denominator is changed toNULL, which will cause theresultto beNULL. The whole result then hasISNULL()to turn any NULLs to 0’s.Personally I would not include the
ISNULL()and leave the result asNULL. But it depends on use-case really.EDIT: Deleted the CASE WHEN version as another answer had it just before mine.