473,385 Members | 2,014 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,385 software developers and data experts.

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 6384
pe********@f2s.com (PeterHardy) wrote in message news:<17**************************@posting.google. 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.google. 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.google. com>...
pe********@f2s.com (PeterHardy) wrote in message news:<17**************************@posting.google. 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(wk3,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********@magicinterface.com (Ed prochak) wrote in message news:<4b**************************@posting.google. com>...
pe********@f2s.com (PeterHardy) wrote in message news:<17**************************@posting.google. 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
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...
3
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...
5
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...
10
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: ...
5
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...
1
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...
1
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...
5
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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...

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.