Solved

Microsoft Access SQL Statement QUERY - Divison by Zero Error

Posted on 2013-01-28
14
460 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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 SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

895 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

13 Experts available now in Live!

Get 1:1 Help Now