Thread: AVG function
I'm using the AVG function to get the average of some integer values of my database, but the result is comming in the integer format too, and I'm loosing some precision. Does someone know how to get the result of the AVG function in a float type, without changing the structure of my table? Thanks, Rodney.
rodneyr@embratel.com.br wrote: > > I'm using the AVG function to get the average of some integer values of my > database, > but the result is comming in the integer format too, and I'm loosing some > precision. > > Does someone know how to get the result of the AVG function in a float type, > without > changing the structure of my table? Try this: hannu=> select * from t; i - 4 4 4 4 3 (5 rows) hannu=> select avg(i * 1.0) from t; avg --- 3.8 (1 row) ------------- Hannu
Hannu Krosing wrote: > rodneyr@embratel.com.br wrote: > > > > I'm using the AVG function to get the average of some integer values of my > > database, > > but the result is comming in the integer format too, and I'm loosing some > > precision. > > > > Does someone know how to get the result of the AVG function in a float type, > > without > > changing the structure of my table? > > Try this: > > hannu=> select * from t; > i > - > 4 > 4 > 4 > 4 > 3 > (5 rows) > > hannu=> select avg(i * 1.0) from t; > avg > --- > 3.8 > (1 row) > Actually, this will work without the need for a multiplication: bga=> select * from t;i-123456(6 rows) bga=> select AVG(i::float) from t;avg---3.5(1 row) -- ____ | Billy G. Allie | Domain....: Bill.Allie@mug.org | /| | 7436 Hartwell | Compuserve: 76337,2061 |-/-|----- | Dearborn, MI 48126| MSN.......: B_G_Allie@email.msn.com |/ |LLIE | (313) 582-1540 |
"Billy G. Allie" wrote: > > Hannu Krosing wrote: > > rodneyr@embratel.com.br wrote: > > > > > Does someone know how to get the result of the AVG function in a float type, > > > without changing the structure of my table? > > > > Try this: > > > > hannu=> select avg(i * 1.0) from t; > > avg > > --- > > 3.8 > > (1 row) > > > Actually, this will work without the need for a multiplication: > > bga=> select AVG(i::float) from t; > avg > --- > 3.5 > (1 row) The ANSI-fied form of which is : hannu=> select avg(cast(i as float)) from t; avg --- 3.8 (1 row) --------------- Hannu