sql server 2008 - SQL divide by zero with a sum function -
i having problems dividing zero. if denominator 0 value zero. when try using nullif, end 0 or 1 calculated value.
here sql:
select statecode, month1date,(sum(order)/sum(value)) myvalue tblorders inner join tblstates on orderstatecode = statecode group statecode, month1date
select statecode, month1date, isnull(sum(order) / nullif(sum(value), 0), 0) myvalue tblorders inner join tblstates on orderstatecode = statecode group statecode, month1date a 0 denominator changed null, cause result null. whole result has isnull() turn nulls 0's.
personally not include isnull() , leave result null. depends on use-case really.
edit: deleted case when version answer had before mine.
Comments
Post a Comment