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

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -