?
Solved

How to avoid divid by zero error

Posted on 2005-04-21
2
Medium Priority
?
376 Views
Last Modified: 2010-03-19
I have a SQL statement that is getting the avg selling price. I'm running into the divide by zero error -- and I'm not sure how to tell the view to just make the percent zero when the denominator is zero.

Here's what I have

SELECT     c.CustId, c.Name, a.PerPost, a.PerPost AS Posted, a.TranType, SUM(a.Qty) AS Nuts, SUM(a.CuryTranAmt) AS Billed, SUM(a.CuryTranAmt) / SUM(a.Qty)
                      AS ASP, a.CpnyID
FROM         dbo.Customer c INNER JOIN
                      dbo.ARTran a ON a.CustId = c.CustId LEFT OUTER JOIN
                      dbo.PJPROJ p ON a.ProjectID = p.project LEFT OUTER JOIN
                      dbo.Inventory i ON a.InvtId = i.InvtID
WHERE     (i.StkItem = 1) AND (a.RefNbr NOT LIKE 'V%') OR
                      (a.Qty <> '0')
GROUP BY c.CustId, c.Name, a.PerPost, a.CpnyID, a.TranType

Thanks
TH1434
0
Comment
Question by:th1434
2 Comments
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1200 total points
ID: 13838104
Please try this:

SELECT     c.CustId, c.Name, a.PerPost, a.PerPost AS Posted, a.TranType, SUM(a.Qty) AS Nuts, SUM(a.CuryTranAmt) AS Billed, CASE WHEN SUM(a.Qty) = 0 THEN 0 ELSE SUM(a.CuryTranAmt) / SUM(a.Qty) END
                      AS ASP, a.CpnyID
0
 
LVL 28

Assisted Solution

by:rafrancisco
rafrancisco earned 800 total points
ID: 13838107
Try this:

SELECT     c.CustId, c.Name, a.PerPost, a.PerPost AS Posted, a.TranType, SUM(a.Qty) AS Nuts, SUM(a.CuryTranAmt) AS Billed, ISNULL(SUM(a.CuryTranAmt) / NULLIF(SUM(a.Qty), 0), 0)
                      AS ASP, a.CpnyID
FROM         dbo.Customer c INNER JOIN
                      dbo.ARTran a ON a.CustId = c.CustId LEFT OUTER JOIN
                      dbo.PJPROJ p ON a.ProjectID = p.project LEFT OUTER JOIN
                      dbo.Inventory i ON a.InvtId = i.InvtID
WHERE     (i.StkItem = 1) AND (a.RefNbr NOT LIKE 'V%') OR
                      (a.Qty <> '0')
GROUP BY c.CustId, c.Name, a.PerPost, a.CpnyID, a.TranType
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

840 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