how to sum up and avg data

I have a rating table, I need to get the average rating for certain record. However , when I use AR method, it did not return the average which is what I need. All it returns are those attributes defined in the model file.

How can I get the average of certain fields? thanks

Answer on your question is statistical query described in Guide.

AFAIK you have to pass MAX(column_name) into select attribute if you want ot achieve max value, AVG(column_name) for average and so one…

I did read that part, however that is only for count . temporarily I used PDO to solve it. However, to keep code persistent, I still what to find out how. Could anybody give me an example? Thanks

You can set ‘select’ option to be things other than COUNT.

I tried to use findBySql('select avg(rating) from …), however the returned object doesn’t have the average value, even if I use

"select avg() as average", still nothing got returned when I print_r the returned object.

Do I need to define a property in the model for average value? I don’t understand your explaination


Irecommend you to add the following to the rules in your model :

 [indent]'average' => array(self::STAT, 'ModelRelated', 'ColumnRelated',

            'select' => 'AVG(FIELD)',[/indent]

It should be really easy to do it like this.