SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Divide by zero error encountered.
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

archana23
Yak Posting Veteran

74 Posts

Posted - 10/31/2013 :  16:21:34  Show Profile  Reply with Quote
Hi,

I have one table with columns patientName , Nurse,ArrivalDate, DepartDate . It has all the patiet information for all the Nurses.

I am calculating Average Number of patients per day per nurse by using below query

SELECT Nurse,
COUNT(DISTINCT patientName) AS NoOfPatients,
COUNT(DISTINCT patientName) * 1.0/COUNT(DISTINCT ArrivalDate) AS [AvgNo.ofpatientsPerDay]
FROM Table t
GROUP BY NURSE

but if ArrivalDate is Null I am getting below error saying that

Divide by zero error encountered.

How can i over come this problem?

Can any one please help me on this?

Thanks

Archana

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 10/31/2013 :  16:23:37  Show Profile  Reply with Quote
one way - if you prefer to get a NULL when arrivalDate is null is:

replace: COUNT(DISTINCT ArrivalDate)
with: NullIf(COUNT(DISTINCT ArrivalDate),0)

Be One with the Optimizer
TG
Go to Top of Page

archana23
Yak Posting Veteran

74 Posts

Posted - 10/31/2013 :  16:27:24  Show Profile  Reply with Quote
I have one more field in the table called DepartDate .

If ArrivalDate is Null i need to check for DepartDate to calculate the AvgNo.ofpatientsPerDay.

Thanks

Archana
Go to Top of Page

TG
Flowing Fount of Yak Knowledge

USA
6062 Posts

Posted - 10/31/2013 :  16:32:30  Show Profile  Reply with Quote
use COALESE function

Be One with the Optimizer
TG
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3586 Posts

Posted - 10/31/2013 :  16:33:49  Show Profile  Reply with Quote
Change TG's code to this:
NullIf(COUNT(DISTINCT COALESCE(ArrivalDate,DepartDate)),0)
Go to Top of Page

archana23
Yak Posting Veteran

74 Posts

Posted - 10/31/2013 :  16:38:26  Show Profile  Reply with Quote
Thank you James :-)

It worked.

Archana
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000