Solved

TSQL SUM Divide BY Zero Error

Posted on 2008-06-20
2
1,426 Views
Last Modified: 2010-04-21
Hi guys

I have this script

SET ARITHIGNORE ON
SET ANSI_WARNINGS OFF
SELECT
calls_bc_d.CompanyId,
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,
bc_Companies.refno1
from Calls_bc_d
left join bc_companies on calls_bc_d.companyid=bc_companies.companyid
where
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

Thanks
0
Comment
Question by:tim_carter
2 Comments
 
LVL 142

Accepted Solution

by:
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,
0
 
LVL 1

Author Closing Comment

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now