Solved

Microsoft Access SQL Statement QUERY - Divison by Zero Error

Posted on 2013-01-28
14
472 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
How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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