TSQL SUM Divide BY Zero Error

Posted on 2008-06-20
Last Modified: 2010-04-21
Hi guys

I have this script

Sum(TakenCount) Calls,
sum(uservalue05)/Sum(TakenCount) AnswerTime,
sum(Talktimetalk) TalkTimeTot,
sum(Talktimetalk)/Sum(TakenCount) TalkTime,
sum(Uservalue39) MsgCount, sum(Uservalue38) emailcount,
sum(Uservalue36) faxcount, sum(Uservalue37) faxudcount,
sum(Uservalue35) SMScount,
from Calls_bc_d
left join bc_companies on calls_bc_d.companyid=bc_companies.companyid
thedate > '13-05-2008'
and thedate < '15-06-2008'
group by calls_bc_d.CompanyId,bc_companies.refno1
order by bc_companies.refno1

I have a couple of things to this.. First of i get a Divide by Zero error, and ofcourse because some of the rows has zero value. But how can i escape that? i mean these two lines

sum(uservalue05)/Sum(TakenCount) AnswerTime,
sum(Talktimetalk)/Sum(TakenCount) TalkTime,

They will return a zero if both columns has a zero value and then return the Divide by ZERO Error. Is there a way to do some kind of IF in this statements? second.. is the first select

Sum(TakenCount) Calls,. How can i do that if this is ZERO that it will return atleast 1. If i just do a
Sum(TakenCount+1) Calls ... Then my problem is that it will do it for everything.. And for some strange reason that if i do it on 0, then Sum(TakenCount+1) Calls will return 3.. All help is much appreciated

Question by:tim_carter
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 500 total points
ID: 21829353
case when sum(TakenCount) = 0 THEN 0 else sum(uservalue05)/Sum(TakenCount) end AnswerTime,
case when Sum(TakenCount) = 0 THEN 0 else sum(Talktimetalk)/Sum(TakenCount) end TalkTime,

Author Closing Comment

ID: 31469076
as always your the best ;) thanks angellll

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question