473,378 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Divide by zero erro

Hi all,

I have a query:

SELECT Data.Region, Data.Dept, Data.Year, Data.Month, Data.Week, Data.Elapsed, Data.[Wp Comp Sales $] AS WpComp, Data.[Cp Comp Sales $] AS CpComp, IIf([LyNumWks]=53 And [Lywk]="JAN,WK4",[LyComp]/2,[LyComp]) AS AdjLyComp, round(([Wp Comp Sales $]-IIf([LyNumWks]=53 And [Lywk]="JAN,WK4",[LyComp]/2,[LyComp]))/IIf([LyNumWks]=53 And [Lywk]="JAN,WK4",[LyComp]/2,[LyComp]),6) AS WpVarLy, round(([Cp Comp Sales $]-IIf([LyNumWks]=53 And [Lywk]="JAN,WK4",[LyComp]/2,[LyComp]))/IIf([LyNumWks]=53 And [Lywk]="JAN,WK4",[LyComp]/2,[LyComp]),6) AS CpVarLy
FROM ((Data INNER JOIN Years ON Data.Year=Years.Year) INNER JOIN LyWk ON (Data.Week=LyWk.TyWk) AND (Data.Year=LyWk.TyYear) AND (Data.Region=LyWk.Region) AND (Data.Dept=LyWk.Dept)) INNER JOIN LYears ON Years.Year=LYears.Year;

essentially it uses iif to divide by two is the row is the last week of a 53 week year.

then, I use the same iif to create a percent to last year's value.

Now, I want to take an average of the percent to last year. But, when I try to do anything (sum, avg, etc) with the data from this query I get a messagebox that says "Divide by zero." The parent query runs fine. I'm really stumped! This is my second query:

SELECT DataAdjLy.Region, Sum(DataAdjLy.WpVarLy) AS WpVarLy
FROM DataAdjLy
GROUP BY DataAdjLy.Region;

As you acn see it is very simple. This query uses the first query (above) and produces the error. The first queyr does not produce the error.
Jan 24 '08 #1
2 1968
sierra7
446 Expert 256MB
Hi
I haven't got time to analyse your code in the first query but you said it worked anyway . . .

What I noticed was "Sum(DataAdjLy.WpVarLy) AS WpVarLy"

I would try "Sum(DataAdjLy.WpVarLy) AS SWpVarLy" changing the output fieldname slightly because I think Access gets confused if you use a field name to hold something which is blatently not the field i.e. 'WpVarLy'

S7
Jan 28 '08 #2
jaxjagfan
254 Expert 100+
Hi all,

I have a query:

SELECT Data.Region, Data.Dept, Data.Year, Data.Month, Data.Week, Data.Elapsed, Data.[Wp Comp Sales $] AS WpComp, Data.[Cp Comp Sales $] AS CpComp, IIf([LyNumWks]=53 And [Lywk]="JAN,WK4",[LyComp]/2,[LyComp]) AS AdjLyComp, round(([Wp Comp Sales $]-IIf([LyNumWks]=53 And [Lywk]="JAN,WK4",[LyComp]/2,[LyComp]))/IIf([LyNumWks]=53 And [Lywk]="JAN,WK4",[LyComp]/2,[LyComp]),6) AS WpVarLy, round(([Cp Comp Sales $]-IIf([LyNumWks]=53 And [Lywk]="JAN,WK4",[LyComp]/2,[LyComp]))/IIf([LyNumWks]=53 And [Lywk]="JAN,WK4",[LyComp]/2,[LyComp]),6) AS CpVarLy
FROM ((Data INNER JOIN Years ON Data.Year=Years.Year) INNER JOIN LyWk ON (Data.Week=LyWk.TyWk) AND (Data.Year=LyWk.TyYear) AND (Data.Region=LyWk.Region) AND (Data.Dept=LyWk.Dept)) INNER JOIN LYears ON Years.Year=LYears.Year;

essentially it uses iif to divide by two is the row is the last week of a 53 week year.

then, I use the same iif to create a percent to last year's value.

Now, I want to take an average of the percent to last year. But, when I try to do anything (sum, avg, etc) with the data from this query I get a messagebox that says "Divide by zero." The parent query runs fine. I'm really stumped! This is my second query:

SELECT DataAdjLy.Region, Sum(DataAdjLy.WpVarLy) AS WpVarLy
FROM DataAdjLy
GROUP BY DataAdjLy.Region;

As you acn see it is very simple. This query uses the first query (above) and produces the error. The first queyr does not produce the error.
Some of your column values are being returned as 0. Within the IIF you will need to include some thing like this:

IIF(MyValue = 0,0,MyFormula)

If those rows are not needed then in the main portion of the SELECT WHERE clause put:
Where MyFieldValue <> 0

Is this the only way you have access to the data? It seems to be in a more complex structure than is necessary. If you are trying to get the weeks difference between 2 dates then use a DateDiff function.

DateDiff("ww", date1, date2)

I see this repeated in your post - [LyNumWks]=53 And [Lywk]="JAN,WK4"

If you are only calcualting values where LyNumWks=53 and Lywk = "JAN,WK4", then put those in the WHERE clause

Select MyFormula1, MyFormula2
From MyDataSource
Where [LyNumWks]=53 And [Lywk]="JAN,WK4"
Jan 28 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Ryan | last post by:
I have the following line in a select statement which comes up with a divide by zero error. CAST(CASE Splinter_Status WHEN 'SUR' THEN 0 ELSE CASE WHEN Sacrifice>=1 THEN...
2
by: Mike Leahy | last post by:
All... I have a query that calculates various using variables from a survey database. As with any survey, there are many instantces of null values. I'm wondering if there is any way to escape...
0
by: Gary Carson | last post by:
Can anyone tell why the query below would throw a divide-by-zero error? The only reason I can see for the error happening would be if SUM() came out to be zero, but this never happens with the...
3
by: Joriveek | last post by:
When I specify a formula between Computed Column Specification, I have two zero values, getting Divide by Zero error, any idea how can I avoid this? I still want SQL Server to display Zero if it is...
4
by: shuisheng | last post by:
Dear all, Assume I have two big arrays A and B. And I want to element-wise divide A by B. When an element of B is zero, the results are also zero. Such as A = { 2, 4, 0, 6} B = { 1, 0, 0, 2}...
5
by: Neo | last post by:
Hi Friends, I am trying following code int main(void) { try { int i,j,k; i=10; j=0;
8
by: =?Utf-8?B?bWljaGFlbGd3ZWllcg==?= | last post by:
Hello! I was working on some code the other day, and I came across an odd discrepancy between the decimal and the double type. If I attempt to divide a decimal by zero, the framework throws an...
1
by: zufie | last post by:
Help! How do I divide by zero on my form (in a text box). That is, how do I carry out a division problem when the denominator is zero? Thanks!, John
1
by: Ajay Indian | last post by:
printf("Divide by zero=%d",1/0); is giving output 1 while x=0,y=1; printf("Divide by zero=%d",y/x);
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.