Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Division by 0




How do I add an if statement to check for sum(n.shippedQtyLy)=0
(sum(n.scanQtyLy)*100)/sum(n.shippedQtyLy) as scanPerQtyLy,

When I try
 if sum(n.shippedQtyLy)=0 then 0 else
(sum(n.scanQtyLy)*100)/sum(n.shippedQtyLy) as scanPerQtyLy,
I get a syntax error


select n.category,d.item_desc,n.bill_id,n.sku_no,n.pricegroup,avg(n.retail_pr) as retail_pr,

sum(n.shippedQtyLy) as shippedQtyLy,sum(n.shippedAmoutLy) as shippedAmoutLy,avg(n.shippedItemPriceLy) as shippedItemPriceLy, --shipped last year      
sum(n.scanQtyLy) as scanQtyLy,sum(n.scanAmoutLy) as scanAmoutLy,avg(n.scItemPriceLy) as scItemPriceLy,                                 --scan last year
(sum(n.scanQtyLy)*100)/sum(n.shippedQtyLy) as scanPerQtyLy,(sum(n.scanAmoutLy)*100)/sum(n.shippedAmoutLy) as scanPerAmountLy,  --scan % last year
0
GadFriedman
Asked:
GadFriedman
1 Solution
 
Rajkumar GsSoftware EngineerCommented:
What is the error ?
0
 
tim_csCommented:
You have to use CASE.

CASE WHEN sum(n.shippedQtyLy) = 0 then 0 else
(sum(n.scanQtyLy)*100)/sum(n.shippedQtyLy) END as scanPerQtyLy
0
 
Rajkumar GsSoftware EngineerCommented:
If it is 'divide by 0' error, try put some check whereever you are diving

Like - Change this
(sum(n.scanQtyLy) * 100) / sum(n.shippedQtyLy) 

Open in new window

to
CASE sum(n.shippedQtyLy) = 0 THEN 0 ELSE
(sum(n.scanQtyLy) * 100) / sum(n.shippedQtyLy) END

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Éric MoreauSenior .Net ConsultantCommented:
select n.category,d.item_desc,n.bill_id,n.sku_no,n.pricegroup,avg(n.retail_pr) as retail_pr
, sum(n.shippedQtyLy) as shippedQtyLy
,sum(n.shippedAmoutLy) as shippedAmoutLy
,avg(n.shippedItemPriceLy) as shippedItemPriceLy
, sum(n.scanQtyLy) as scanQtyLy
,sum(n.scanAmoutLy) as scanAmoutLy
,avg(n.scItemPriceLy) as scItemPriceLy
, case when isnull(sum(n.shippedQtyLy),0) = 0 then 0 else (sum(n.scanQtyLy)*100)/sum(n.shippedQtyLy) end as scanPerQtyLy
, case when isnull(sum(n.shippedAmoutLy),0) then 0 else (sum(n.scanAmoutLy)*100)/sum(n.shippedAmoutLy) end as scanPerAmountLy
0
 
Lee SavidgeCommented:
You could try this:

(sum(n.scanQtyLy)*100)/isnull(nullif(sum(sum(n.shippedQtyLy)), 0.0), 1.0)as scanPerQtyLy,
0
 
Lee SavidgeCommented:
Whoops, I mean this:

(sum(n.scanQtyLy)*100)/isnull(nullif(sum(n.shippedQtyLy), 0.0), 1.0)as scanPerQtyLy
0
 
GadFriedmanAuthor Commented:
Thank You
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now