dbTalk Databases Forums  

Calculating Average on columns with zeros.

comp.databases.oracle comp.databases.oracle


Discuss Calculating Average on columns with zeros. in the comp.databases.oracle forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
PeterHardy
 
Posts: n/a

Default Calculating Average on columns with zeros. - 08-05-2004 , 03:49 AM






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.

Reply With Quote
  #2  
Old   
Mark D Powell
 
Posts: n/a

Default Re: Calculating Average on columns with zeros. - 08-05-2004 , 09:07 AM






peterhardy (AT) f2s (DOT) com (PeterHardy) wrote in message news:<17736c9c.0408050049.565668e5 (AT) posting (DOT) google.com>...
Quote:
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 --


Reply With Quote
  #3  
Old   
Ed prochak
 
Posts: n/a

Default Re: Calculating Average on columns with zeros. - 08-05-2004 , 11:23 AM



peterhardy (AT) f2s (DOT) com (PeterHardy) wrote in message news:<17736c9c.0408050049.565668e5 (AT) posting (DOT) google.com>...
Quote:
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


Reply With Quote
  #4  
Old   
PeterHardy
 
Posts: n/a

Default Re: Calculating Average on columns with zeros. - 08-05-2004 , 01:54 PM



Mark.Powell (AT) eds (DOT) com (Mark D Powell) wrote in message news:<2687bb95.0408050607.2cd6a38a (AT) posting (DOT) google.com>...
Quote:
peterhardy (AT) f2s (DOT) com (PeterHardy) wrote in message news:<17736c9c.0408050049.565668e5 (AT) posting (DOT) 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.


Reply With Quote
  #5  
Old   
Kevin
 
Posts: n/a

Default Re: Calculating Average on columns with zeros. - 08-09-2004 , 12:55 PM



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


Reply With Quote
  #6  
Old   
Adem
 
Posts: n/a

Default Re: Calculating Average on columns with zeros. - 08-10-2004 , 07:07 AM



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.prochak (AT) magicinterface (DOT) com (Ed prochak) wrote in message news:<4b5394b2.0408050823.4de2bb81 (AT) posting (DOT) google.com>...
Quote:
peterhardy (AT) f2s (DOT) com (PeterHardy) wrote in message news:<17736c9c.0408050049.565668e5 (AT) posting (DOT) 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

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.