Solved

Microsoft Access SQL Statement QUERY - Divison by Zero Error

Posted on 2013-01-28
14
452 Views
Last Modified: 2013-01-28
When I run this SQL Code Below in Access I get this error. "Division by Zero".  The Query that is giving me the error is in Bold.

Select SUM(Total-Tax) as [Sales], SUM(([Total]-[Tax]-[T_Cost]-[P_Cost])/([Total]-[Tax]-[Supplies])) as [GPM]
 From HRO Where ([PAY_DATE] >= #1/20/2013 12:00:00 AM#) And ([PAY_DATE] <= #1/26/2013 12:00:00 AM#) and [Status] = 'C'

Any suggestions?
0
Comment
Question by:sj77
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 6

Expert Comment

by:CaptainGiblets
ID: 38827601
either ([Total]-[Tax]-[T_Cost]-[P_Cost]) or ([Total]-[Tax]-[Supplies]) ends up with a value of 0, you then try to divide by it and its not possible, you cant divide by 0 which is why it is throwing the error.

You will either have to identify why it is pulling through 0 or put some code in to check if the fields are 0 before you try to divide by them.
0
 
LVL 4

Expert Comment

by:MimicTech
ID: 38827632
check for zero.

select
 SUM(Total-Tax) as [Sales]
,iif(([Total]-[Tax]-[Supplies])<>0, SUM(([Total]-[Tax]-[T_Cost]-[P_Cost])/([Total]-[Tax]-[Supplies]), 0) as [GPM]
from HRO
where ([PAY_DATE] >= #1/20/2013 12:00:00 AM#)
     and ([PAY_DATE] <= #1/26/2013 12:00:00 AM#)
     and [Status] = 'C'
0
 

Author Comment

by:sj77
ID: 38827634
I understand but do you know if there is some way to do a CASE WHEN Statement?
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 38827639
The standard way to deal with this scenario is...

If (denominator = 0, 0, numerator / denominator) as the_name

... so in your case  (air code, so don't shoot me) ...

SUM( IF ([Total]-[Tax]-[Supplies]) = 0, 0, [Total]-[Tax]-[T_Cost]-[P_Cost])/([Total]-[Tax]-[Supplies])) as [GPM]

This also begs the question if your detail section should have three separate text boxes, one for the numerator, one for the denominator, and one for If (denominator = 0, 0, numerator / denominator) as the_name, then your footer can just sum the the_name columns.
0
 
LVL 4

Expert Comment

by:MimicTech
ID: 38827642
sorry did not notice the SUM on GPM

you could:

select
 SUM(Total-Tax) as [Sales]
,SUM(([Total]-[Tax]-[T_Cost]-[P_Cost])/([Total]-[Tax]-[Supplies])) as [GPM]
from HRO
where ([PAY_DATE] >= #1/20/2013 12:00:00 AM#)
     and ([PAY_DATE] <= #1/26/2013 12:00:00 AM#)
     and [Status] = 'C'
having ([Total]-[Tax]-[Supplies]) <> 0
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 38827645
<SQL Server version of above using CASE>

CASE WHEN denominator = 0 THEN 0 ELSE numerator / denominator END

btw, in both cases you'll have to handle any NULL values.
0
 

Author Comment

by:sj77
ID: 38827650
Thanks,

MimicTech

select
 SUM(Total-Tax) as [Sales]
,iif(([Total]-[Tax]-[Supplies])<>0, SUM(([Total]-[Tax]-[T_Cost]-[P_Cost])/([Total]-[Tax]-[Supplies]), 0) as [GPM]
from HRO
where ([PAY_DATE] >= #1/20/2013 12:00:00 AM#)
     and ([PAY_DATE] <= #1/26/2013 12:00:00 AM#)
     and [Status] = 'C

Do you mean for that  "iif"
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 4

Expert Comment

by:MimicTech
ID: 38827655
please disregard my second post - there is no grouping on this query -- use iif not having clause
0
 

Author Comment

by:sj77
ID: 38827685
MimicTech,

That helps but it eliminates some information which I am also Querying,

Count(RO_NO) as [Count]

How would you suggest that I include those 4 "RO_NO" that having ([Total]-[Tax]-[Supplies]) <> 0 eliminates?
0
 
LVL 4

Expert Comment

by:MimicTech
ID: 38827723
sj77,

Please clarity the count question. I don't see the "RO_NO" in the initial question. Please repost query with that portion or let give more detail on what you would want added, and I might be able to help.
0
 

Author Comment

by:sj77
ID: 38827770
MimicTech,

I think I have a work around..

I will do the following:
Select
SUM(Total) as Total, Sum(Tax) as Tax, Sum(T_Cost) as T_Cost, Sum(P_Cost) as P_Cost, Sum(Supplies) as Supplies.  But I want to do the following in one line:

(Sum(Total) - Sum(Tax)-Sum(T_Cost)-Sum(P_Cost) / Sum(Total) - Sum(Tax) - Sum(Supplies)) as GRM

Please clean up and advise how I would do the above formula.
0
 
LVL 4

Expert Comment

by:MimicTech
ID: 38827849
would this work for you?

select
 Sum(Total) as Total
,Sum(Tax) as Tax
,Sum(T_Cost) as T_Cost
,Sum(P_Cost) as P_Cost,
,Sum(Total-Tax) as [Sales]

,Sum(iif(  ([Total]-[Tax]-[Supplies])<>0
             ,(([Total]-[Tax]-[T_Cost]-[P_Cost])/([Total]-[Tax]-[Supplies]))
             ,0)
) as [GPM]

from HRO
where ([PAY_DATE] >= #1/20/2013 12:00:00 AM#)
     and ([PAY_DATE] <= #1/26/2013 12:00:00 AM#)
     and [Status] = 'C
0
 

Author Comment

by:sj77
ID: 38827967
Error Message: Circular reference caused by alias 'Total' in query definition's SELECT list.

select
 Sum(Total) as Total,
Sum(Tax) as Tax,
Sum(T_Cost) as T_Cost,
Sum(P_Cost) as P_Cost,
Sum(Total-Tax) as [Sales] ,
Sum(iif(  ([Total]-[Tax]-[Supplies])<>0
             ,(([Total]-[Tax]-[T_Cost]-[P_Cost])/([Total]-[Tax]-[Supplies]))
             ,0)
) as [GPM]

from HRO
where ([PAY_DATE] >= #1/20/2013 12:00:00 AM#)
     and ([PAY_DATE] <= #1/26/2013 12:00:00 AM#)
     and [Status] = 'C'
0
 
LVL 4

Accepted Solution

by:
MimicTech earned 500 total points
ID: 38827985
Change alias names to not be the same as any real column name. Made names plural.

select
 Sum(Total) as Totals,
Sum(Tax) as Taxes,
Sum(T_Cost) as T_Costs,
Sum(P_Cost) as P_Costs,
Sum(Total-Tax) as [Sales] ,
Sum(iif(  ([Total]-[Tax]-[Supplies])<>0
             ,(([Total]-[Tax]-[T_Cost]-[P_Cost])/([Total]-[Tax]-[Supplies]))
             ,0)
) as [GPM]

from HRO
where ([PAY_DATE] >= #1/20/2013 12:00:00 AM#)
     and ([PAY_DATE] <= #1/26/2013 12:00:00 AM#)
     and [Status] = 'C'
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

759 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

20 Experts available now in Live!

Get 1:1 Help Now