Divide by zero exception of average of columns in a row in SQL Server -


i have code made calculate average of row in table. problem sum of mintwee, mineen, nul, pluseen , plustwee (are column names) equal zero, gives me divide 0 exception (what normal of course). how can protect it given't that? if sum equal 0 average equal 0 of row. use sql server 2014.

select top 5      id, mintwee, mineen, nul, pluseen, plustwee, naam      topic       categorieid = 7 , verwijderd = 0  order      round(cast((mintwee * (-2) + mineen * (-1) + nul * 0 + pluseen * 1 + plustwee * 2) float) / (mintwee + mineen + nul + pluseen + plustwee), 1) desc, creatie desc 

you can test 0 value case when

select top 5 id, mintwee, mineen, nul, pluseen, plustwee, naam topic  categorieid = 7 , verwijderd = 0  order     case when (mintwee + mineen + nul + pluseen + plustwee) = 0  creatie    else    round(cast((mintwee * (-2) + mineen * (-1) + nul * 0 + pluseen * 1 + plustwee * 2) float) / (mintwee + mineen + nul + pluseen + plustwee), 1)    end     desc,     creatie desc 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -