473,799 Members | 2,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculating Average on columns with zeros.

I'm trying to work out an average field on a report that i'm writing
and having the problem described below.

e.g. if you had 3 weeks of cash which were 0, 2, 4 then a normal
average
would be ((0+2+4)/3) = 2 but the average that I want is (2+4)/2 = 3.

Now I can do this in SQL by using AVG(cash) with a WHERE cash>0. But
the
trouble comes when all cash values are zero for all all columns on the
report.

e.g. if I had
wk1 wk2 wk3
rowA 0 2 4
rowB 0 0 0
rowC 1 0 0

I am using

SELECT name, SUM(cash), AVG(cash) from TABLE where cash>0;

And I get
rowA, 6, 3
rowC, 1, 1

This is correct for those rows but I want a result for rowB that has

rowB, 0, 0

Can anyone help with this.
Jul 19 '05 #1
5 6417
pe********@f2s. com (PeterHardy) wrote in message news:<17******* *************** ****@posting.go ogle.com>...
I'm trying to work out an average field on a report that i'm writing
and having the problem described below.

e.g. if you had 3 weeks of cash which were 0, 2, 4 then a normal
average
would be ((0+2+4)/3) = 2 but the average that I want is (2+4)/2 = 3.

Now I can do this in SQL by using AVG(cash) with a WHERE cash>0. But
the
trouble comes when all cash values are zero for all all columns on the
report.

e.g. if I had
wk1 wk2 wk3
rowA 0 2 4
rowB 0 0 0
rowC 1 0 0

I am using

SELECT name, SUM(cash), AVG(cash) from TABLE where cash>0;

And I get
rowA, 6, 3
rowC, 1, 1

This is correct for those rows but I want a result for rowB that has

rowB, 0, 0

Can anyone help with this.


Peter, the SQL you posted calculates the sum for a column on rows of
data while the description is summing the columns on a single row.
Which is it that you actually want? [A + B + C or summation of A for
the row set]

HTH -- Mark D Powell --
Jul 19 '05 #2
pe********@f2s. com (PeterHardy) wrote in message news:<17******* *************** ****@posting.go ogle.com>...
I'm trying to work out an average field on a report that i'm writing
and having the problem described below.

e.g. if you had 3 weeks of cash which were 0, 2, 4 then a normal
average
would be ((0+2+4)/3) = 2 but the average that I want is (2+4)/2 = 3.

Now I can do this in SQL by using AVG(cash) with a WHERE cash>0. But
the
trouble comes when all cash values are zero for all all columns on the
report.

e.g. if I had
wk1 wk2 wk3
rowA 0 2 4
rowB 0 0 0
rowC 1 0 0

I am using

SELECT name, SUM(cash), AVG(cash) from TABLE where cash>0;

And I get
rowA, 6, 3
rowC, 1, 1

This is correct for those rows but I want a result for rowB that has

rowB, 0, 0

Can anyone help with this.

you'll have to special case that condition. something like this should do it:
SELECT name, SUM(cash), 0 FROM table GROUP BY name having SUM(cash) = 0;

then you just need a UNION.

HTH,
Ed
Jul 19 '05 #3
Ma*********@eds .com (Mark D Powell) wrote in message news:<26******* *************** ****@posting.go ogle.com>...
pe********@f2s. com (PeterHardy) wrote in message news:<17******* *************** ****@posting.go ogle.com>...
I'm trying to work out an average field on a report that i'm writing
and having the problem described below.

e.g. if you had 3 weeks of cash which were 0, 2, 4 then a normal
average
would be ((0+2+4)/3) = 2 but the average that I want is (2+4)/2 = 3.

Now I can do this in SQL by using AVG(cash) with a WHERE cash>0. But
the
trouble comes when all cash values are zero for all all columns on the
report.

e.g. if I had
wk1 wk2 wk3
rowA 0 2 4
rowB 0 0 0
rowC 1 0 0

I am using

SELECT name, SUM(cash), AVG(cash) from TABLE where cash>0;

And I get
rowA, 6, 3
rowC, 1, 1

This is correct for those rows but I want a result for rowB that has

rowB, 0, 0

Can anyone help with this.


Peter, the SQL you posted calculates the sum for a column on rows of
data while the description is summing the columns on a single row.
Which is it that you actually want? [A + B + C or summation of A for
the row set]

HTH -- Mark D Powell --

It is a summation of the row for the row set that i require.
Jul 19 '05 #4
> > > e.g. if I had
wk1 wk2 wk3
rowA 0 2 4
rowB 0 0 0
rowC 1 0 0

I am using

SELECT name, SUM(cash), AVG(cash) from TABLE where cash>0;

And I get
rowA, 6, 3
rowC, 1, 1

This is correct for those rows but I want a result for rowB that has

rowB, 0, 0

Can anyone help with this.


For the way you describe the situation here, you simply need:
SELECT my_tab.*,
(wk1+wk2+wk3) /
DECODE(wk1+wk2+ wk3,0,1,
(DECODE(wk1,0,0 ,1)+DECODE(wk2, 0,0,1)+DECODE(w k3,0,0,1)))
avg_value,
(wk1+wk2+wk3) total_value
FROM my_tab
Jul 19 '05 #5
Hi Peter,

Try this!
will work out OK I think...

Select Sum(Cash) / Sum(Case When Cash > 0 Then 1 Else 0 End)
From Table

cheers, Adem

ed********@magi cinterface.com (Ed prochak) wrote in message news:<4b******* *************** ****@posting.go ogle.com>...
pe********@f2s. com (PeterHardy) wrote in message news:<17******* *************** ****@posting.go ogle.com>...
I'm trying to work out an average field on a report that i'm writing
and having the problem described below.

e.g. if you had 3 weeks of cash which were 0, 2, 4 then a normal
average
would be ((0+2+4)/3) = 2 but the average that I want is (2+4)/2 = 3.

Now I can do this in SQL by using AVG(cash) with a WHERE cash>0. But
the
trouble comes when all cash values are zero for all all columns on the
report.

e.g. if I had
wk1 wk2 wk3
rowA 0 2 4
rowB 0 0 0
rowC 1 0 0

I am using

SELECT name, SUM(cash), AVG(cash) from TABLE where cash>0;

And I get
rowA, 6, 3
rowC, 1, 1

This is correct for those rows but I want a result for rowB that has

rowB, 0, 0

Can anyone help with this.

you'll have to special case that condition. something like this should do it:
SELECT name, SUM(cash), 0 FROM table GROUP BY name having SUM(cash) = 0;

then you just need a UNION.

HTH,
Ed

Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
15817
by: Jesse Wolgamott | last post by:
I've seen plenty of posts regarding the estimation of table size, usually in the processing of planning for server storage needs. Well, I've got a different problem. I need to know how much data each of our Customers are using in a Database. (1 SQL DB stores multiple customers). Basically, I want to be able to say: Customer A: 45.5 MB, Customer B: 655 MB.
3
5802
by: GregM | last post by:
Hi, I'm hoping that someone can point me in the right direction with this. What I would like to do is calculate the average time it takes to load a page. I've been searching the net and reading lots but I haven't found anything that helps too much. I'm testing our web site and hiting +6000 urls per test. Here is a subset of what I'm doing. import IEC #IE controller from http://www.mayukhbose.com/python/IEC/index.php from...
5
4175
by: Mr. Ken | last post by:
I am calculating the phase of an IQ signal, which are polluted by AWGN gaussian noise. Thus, near pi/2, direct division of atan(Q/I) may yield outputs either +pi/2 or -pi/2. How do I handle this situation? Thanks.
10
11603
by: Joseph Geretz | last post by:
I need to calculate miles per degree longitude, which obviously depends on latitude since lines of longitude converge at the poles. Via Google, I've come up with the following calculation: private const double MilesPerDegLat = 69.04; private const double EarthRadiusMiles = 3963.1676; private static double PiDiv180 = Math.PI / 180; double MilesPerDegLon = MilesPerDegLat * Math.Cos(Latitude * PiDiv180)
5
2017
by: ye2127 | last post by:
Hi, I have two fields in my report. One of them is school name(the school name appears multiple times). The other field is class test score. How would I go about calculating the test average for each school? I can calculate the average of all the schools but am having difficulty coding some kind of sort procedure for an average test score of all the classes in each school. Thanks...
1
1519
by: ye2127 | last post by:
Hi, I have two fields in my report. One of them is school name(the school name appears multiple times). The other field is class test score. How would I go about calculating the test average for each school?(In a report) I can calculate the average of all the schools but am having difficulty coding some kind of sort procedure for an average test score of all the classes in each school. Does someone know the code for such a procedure?...
1
3517
kmartinenko
by: kmartinenko | last post by:
I have a table with over 12,000 entries. I have created a form (with the help of this forum) that will return the search results based upon the stop and stop time selected. See post http://www.thescripts.com/forum/thread757933.html for additional context. Now, I want to take this a step further by querying this table by clicking a new command button on the form which will open up a new query displaying the average number for six of these...
5
869
by: PeterHardy | last post by:
I'm trying to work out an average field on a report that i'm writing and having the problem described below. e.g. if you had 3 weeks of cash which were 0, 2, 4 then a normal average would be ((0+2+4)/3) = 2 but the average that I want is (2+4)/2 = 3. Now I can do this in SQL by using AVG(cash) with a WHERE cash>0. But the trouble comes when all cash values are zero for all all columns on the
1
3554
by: cmb3587 | last post by:
My code runs fine for the most part...the only time it fails is when I type in a negative to end the array. I don't want the negative number to be included in the array and I thought that is what the while loop in the getNums method does. However, it is including this negative number when calculating the avg and it is doing something weird to the median. When the array end itself because the max size has been reached everything works fine. ...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6804
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.