Solved

how 2 catch divide by zero

Posted on 2000-03-30
11
511 Views
Last Modified: 2012-06-21
I am trying to create a view; one of the columns is meant to be a calculated profit percentage for the month to date sales.

e.g.

MONTH_PROF_PERC =
(i.SALES_CURMONTH - i.SALES_COST_CURMONTH) / i.SALES_CURMONTH

[i is an alias for inventory table]
However, sometimes a product hasn't sold anything for that month, so the view barfs with a divide by zero error, and hence nothing shows up.

in a real language (I hate SQL) I would simply say something like:

MONTH_PROF_PERC =
if( i.SALES_CURMONTH > 0)
    then
(i.SALES_CURMONTH - i.SALES_COST_CURMONTH) / i.SALES_CURMONTH
    else
0

There must be a way to code this using SQL, perhaps as a subquery? NOTE: I can't add a where clause that prevents records where  i.SALES_CURMONTH <= 0 from showing up, that would drop certain products that have been selling great all year, but maybe not yet this month.

Thanks!

p.s. I'm using Sybase Adaptive Server Enterprise 11.9.2
0
Comment
Question by:stoneman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 

Expert Comment

by:Alex_nl
ID: 2672193
Could you use a case statement here?

Something like:

MONTH_PROF_PERC = CASE i.SALES_CURMONTH WHEN 0 THEN 0 ELSE (i.SALES_CURMONTH - i.SALES_COST_CURMONTH) / i.SALES_CURMONTH END

There may be a better syntax for this but I don't have the Online Books installed here...

HTH,
Alex
0
 

Expert Comment

by:csabay
ID: 2672851
Hi,

there is also an if-syntax to use in Sybase:

if i.sales_curmonth = 0
    month_prof_perc = 0
else
    month_prof_perc = ... &c

Charles
0
 
LVL 1

Author Comment

by:stoneman
ID: 2676994
Sorry, neither of these suggestions helped. The whole context of what I am trying to do is here:

CREATE VIEW INV_ANALYSIS
AS
SELECT  i.VENDOR_6,  
        i.SKU,  
        MONTH_PROFIT = (i.SALES_CURMONTH - i.SALES_COST_CURMONTH),
        MONTH_PROF_PERC = (i.SALES_CURMONTH - i.SALES_COST_CURMONTH) / i.SALES_CURMONTH
FROM INVENTORY i
WHERE i.SALES_CURYEAR > 0


When I put either of the above into suggestions into the line for MONTH_PROF_PERC I get a syntax error near keyword if.




0
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 

Expert Comment

by:Alex_nl
ID: 2679123
Hi stoneman,
I'm pretty sure this will work:

CREATE VIEW INV_ANALYSIS
AS
SELECT  i.VENDOR_6,  
        i.SKU,  
        MONTH_PROFIT = (i.SALES_CURMONTH - i.SALES_COST_CURMONTH),
        MONTH_PROF_PERC = CASE i.SALES_CURMONTH
                                                      WHEN 0 THEN 0 ELSE
                                                            (i.SALES_CURMONTH - i.SALES_COST_CURMONTH) / i.SALES_CURMONTH
                                                      END
FROM INVENTORY i
WHERE i.SALES_CURYEAR > 0
0
 

Expert Comment

by:shabbirab
ID: 2770635
Just add a condition as under and this should work.

MONTH_PROF_PERC = (i.SALES_CURMONTH - i.SALES_COST_CURMONTH) / ISNULL(i.SALES_CURMONTH , 1)
AND MONTH_PROF_PERC != (i.SALES_CURMONTH - i.SALES_COST_CURMONTH)
0
 

Expert Comment

by:shabbirab
ID: 2770641
A little too fast on that ..
Give me some time.

0
 

Expert Comment

by:shabbirab
ID: 2770677
A little too fast on that ..
Give me some time.

0
 
LVL 1

Author Comment

by:stoneman
ID: 2829408
None of these suggestions worked, sybase wouldn't
t let me save the create view. I've resorted to performing a check in my Java code instead. Thanks for the tries though!

0
 
LVL 3

Expert Comment

by:knel1234
ID: 2872278
stoneman,

Are you still looking for an answer?

knel1234
0
 
LVL 1

Author Comment

by:stoneman
ID: 2913088
Sure, but I'm convinced at this point it can't be done. My solution was to add a SALES_CURMONTH > 0 to the where clause. This stopped records that could produce the divide by zero, but my client wasn't too happy. It means items that are still top sellers, but for some reason haven't sold anything this month yet, would not show up in the report. If you can figure it out I'd double the points.

-stoneman
0
 

Accepted Solution

by:
shabbirab earned 40 total points
ID: 2915433
If you do a Union of the Same Query with the condition SALES_CURMONTH <= 0, it should work. I mean ...

SELECT ....
MONTH_PROF_PERC =  (i.SALES_CURMONTH - i.SALES_COST_CURMONTH) / i.SALES_CURMONTH
FROM ...
WHERE ...
AND SALES_CURMONTH > 0
UNION
SELECT ....
MONTH_PROF_PERC =  0
FROM ...
WHERE ...
AND SALES_CURMONTH <= 0
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Update query 9 364
performance measure of  running SSIS package 4 227
Sysbase Central connection fails 3 77
sybase output only middle rows 4 44
While it may be true that the internet is a place of possibilities, it is also a hostile environment lurking with many dangers. By clicking on the wrong link, trusting the wrong person or using a weak password, you are virtually inviting hackers to …
Arrow Electronics was searching for a KVM  (Keyboard/Video/Mouse) switch that could display on one single monitor the current status of all units being tested on the rack.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 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