SQL AVG problem
Hi,
I am trying to average the values of 4 columns in my DB but instead of returning the average it returns the sum of these.
rs3 = statement3.executeQuery("SELECT AVG(Satisfaction+Quality+Communication+Delivery) FROM Ratings WHERE SupplierID='"+SupplierID+"' AND AuctionID='"+LotID+"'");
rs3.next();
out.print(rs3.getInt(1));
any ideas?
[378 byte] By [
haggard17a] at [2007-11-27 11:41:53]

> shouldn't the AVG do that?
No, it is an aggregate function; it returns the average of the column for all the rows:
SELECT AVG(a) FROM
(SELECT 1 a FROM DUAL
UNION ALL
SELECT 2 FROM DUAL
UNION ALL
SELECT 3 FROM DUAL)
AVG(A)
-
2
1 row selected
Message was edited by:
jbish
jbisha at 2007-7-29 17:40:52 >

i changed it to this:
rs3 = statement3.executeQuery("SELECT (Satisfaction+Quality+Communication+Delivery)/4 FROM Ratings WHERE SupplierID='"+SupplierID+"' AND AuctionID='"+LotID+"'");
rs3.next();
out.print(rs3.getDouble(1));
however, with the values 3,2,4,5 it gives the reult of 3.0 but should be 3.5.
any idea why?
Because the AVG function returns the result in the same datatype as the variables passed in.
You are passing in a load of integers (full numbers) and as such the AVG function returns the nearest whole number i.e. 3
Again I say this JAVA forum is not the place for SQL questions. Try a SQL forum
> Because the AVG function returns the result in the
> same datatype as the variables passed in.
>
The OP was not using the AVG function in the last post.
> You are passing in a load of integers (full numbers)
> and as such the AVG function returns the nearest
> whole number i.e. 3
>
The OP needs to say what type of database is being used; in Oracle this would work:
SELECT (2+2+3)/3 a FROM DUAL
A
-
2.33333333
1 row selected
> Again I say this JAVA forum is not the place for SQL
> questions. Try a SQL forum
A good suggestion.
jbisha at 2007-7-29 17:40:52 >
