Author |
Topic |
cesark
Posting Yak Master
215 Posts |
Posted - 2007-10-30 : 06:09:16
|
I am trying to get a simple average of 'PublOffers' column which is smallint data type, but I get always zero value as average, and this is not correct since there are values different to zero in that column for the specified month and year. Somebody knows which can be de problem?This is the simple code I use: SELECT AVG(PublOffers) As myAverage FROM PublOffersControl WHERE MONTH(Data) = 3 And YEAR(Data) = 2006 Thanks |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-10-30 : 06:20:34
|
Please post the sample data.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
cesark
Posting Yak Master
215 Posts |
Posted - 2007-10-30 : 06:46:49
|
If I use this Select: SELECT User_num As User, sum(PublOffers) as Offers, avg(PublOffers) As myAverage FROM PublOffersControl WHERE MONTH(Data) = 3 And YEAR(Data) = 2006 GROUP BY User_num I get this data:User Offers myAverage284 0 0299 0 0246 0 0285 0 0260 0 0331 0 0316 0 0277 4 0323 1 0292 0 0339 0 0300 0 0332 0 0232 0 0317 0 0324 0 0340 0 0262 0 0333 3 0325 0 0318 0 0288 5 0326 0 0319 0 0342 0 0312 0 0335 0 0281 0 0327 0 0304 0 0343 0 0282 3 0305 0 0236 0 0259 0 0328 0 0344 2 0289 0 0283 0 0314 0 0275 0 0337 0 0345 0 0 |
 |
|
Koji Matsumura
Posting Yak Master
141 Posts |
Posted - 2007-10-30 : 07:21:56
|
Try1.0 * AVG(PublOffers) |
 |
|
cesark
Posting Yak Master
215 Posts |
Posted - 2007-10-30 : 07:35:57
|
Hi, if I do this I get '.0' instead of '0'. It's still incorrect |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-10-30 : 07:50:56
|
" Try1.0 * AVG(PublOffers)"I think you need AVG(PublOffers * 1.0)to increase the precision, or perhaps:AVG(CONVERT(float, PublOffers))Kristen |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-10-30 : 08:23:09
|
Some operations involved on INT column(division, average, etc) need the column converted to float or multiplied by 1.0. I wonder why there is no implicit conversion on these operations(as done by other RDBMSs)MadhivananFailing to plan is Planning to fail |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-10-30 : 08:50:41
|
quote: Originally posted by madhivanan Some operations involved on INT column(division, average, etc) need the column converted to float or multiplied by 1.0. I wonder why there is no implicit conversion on these operations(as done by other RDBMSs)MadhivananFailing to plan is Planning to fail
I think that's a good thing. If I perform an operation on two integers, an integer should be returned unless I explicitly ask for something else.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-10-30 : 09:34:52
|
quote: Originally posted by jsmith8858
quote: Originally posted by madhivanan Some operations involved on INT column(division, average, etc) need the column converted to float or multiplied by 1.0. I wonder why there is no implicit conversion on these operations(as done by other RDBMSs)MadhivananFailing to plan is Planning to fail
I think that's a good thing. If I perform an operation on two integers, an integer should be returned unless I explicitly ask for something else.- Jeffhttp://weblogs.sqlteam.com/JeffS
Yes. SQL Server uniquely stands with some features like this Also, In SQL Server, unique column will have only one NULL whereas other RDMSs allow more than one NULLs (Colleagues used to debate on this )MadhivananFailing to plan is Planning to fail |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-10-30 : 10:24:11
|
And I've made views WHERE MyColumn IS NOT NULL and then indexed on that - and then discovered there were undesirable side effects of that, so then implemented trigger to enforce uniqueness of not-null column values instead ...Kristen |
 |
|
cesark
Posting Yak Master
215 Posts |
Posted - 2007-10-30 : 10:58:35
|
I can't still discuss about this matter , but it works!! I converted to float as Kristen said, and a very precision number is returned (this time the correct average). If I use 'AVG(PublOffers * 1.0)' only a few digits are returned after the decimal point, I suppose it's a question of choice or kind of use of that result.Thank you!! |
 |
|
Kristen
Test
22859 Posts |
Posted - 2007-10-30 : 11:36:23
|
"I suppose it's a question of choice or kind of use of that result"Yup. You could useSELECT ..., ROUND(avg(PublOffers), nnnn) As myAverageROUND ...if you want to reduce the precision after the average is calculatedKristen |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-30 : 13:42:06
|
If you have an index on column Data, you should try out this WHERE clause SELECT AVG(PublOffers) As myAverageFROM PublOffersControl WHERE Data >= '20060301' AND Data < '20060401' E 12°55'05.25"N 56°04'39.16" |
 |
|
|