Solved

Microsoft Access SQL Statement QUERY - Divison by Zero Error

Posted on 2013-01-28
14
462 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
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.

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

786 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