Yii Framework Forum: how to sum up and avg data - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

how to sum up and avg data Rate Topic: -----

#1 User is offline   xzhang 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 09-July 09

Posted 27 July 2009 - 07:56 PM

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
0

#2 User is offline   aztech 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 206
  • Joined: 12-December 08
  • Location:Poland

Posted 28 July 2009 - 02:31 AM

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'm not complete idiot... some parts are missing!
0

#3 User is offline   xzhang 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 09-July 09

Posted 28 July 2009 - 07:42 AM

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
0

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,899
  • Joined: 04-October 08
  • Location:DC, USA

Posted 29 July 2009 - 04:24 PM

You can set 'select' option to be things other than COUNT.
0

#5 User is offline   xzhang 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 09-July 09

Posted 01 August 2009 - 09:53 PM

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

thanks
0

#6 User is offline   CTala 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 94
  • Joined: 17-August 11
  • Location:Chile

Posted 29 November 2011 - 11:58 AM

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

'average' => array(self::STAT, 'ModelRelated', 'ColumnRelated',
'select' => 'AVG(FIELD)',


It should be really easy to do it like this.
--
MSc. Cristian Tala Sánchez
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users