Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Microsoft Access SQL Statement QUERY - Divison by Zero Error

Posted on 2013-01-28
14
Medium Priority
?
473 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
[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
  • 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
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 
LVL 66

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 66

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
 
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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

721 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