Display average from a column? EDITED: This has been altered to include the code suggested by Dooshta.

I need to fill a computed field with the average value of all entries in a certain field, and only from nodes that have a specific node reference field.

What I have right now in my Computed Field is:

$nids = db_query('
  SELECT entity_id FROM {field_revision_field_venue_reference_review}
  WHERE field_venue_reference_review_target_id = :nid',
  array(':nid' =&gt; $entity-&gt;nid)
)-&gt;fetchCol();

$entity_field[0]['value'] = db_query('
   SELECT AVG(field_payment_review_value) FROM {field_data_field_payment_review}
   WHERE entity_id IN (:nids)', 
   array(':nids' =&gt; $nids)
)-&gt;fetchField();


This is working, and displays the correct average from the correct nodes. However, I get a PODexception error on some pages that do not include the above code. This is confusing to me, because it seems that if there is bad syntax, then I should get the error on the page using the code as well as pages not using the code, right? Either that or it just shouldn't work.



As requested by Booshta, here are screenshots of the tables I'm querying.
The first is the table containing the entity reference field in the $nids part of the query


The second is the table containg the values being averaged in the $entity_field part of the query
I'm not familiar with the computed field module and not sure I completely understand the relationships between your nodes and fields, but here's about how you should be able to get things working.

The first query seems like it would be alright if you'd remove that extra closing bracket and used fetchField() to retrieve the value:

$entity_field[0]['value'] = db_query('
   SELECT AVG(field_payment_review_value) FROM {field_data_field_payment_review}
')-&gt;fetchField();


The second one needs some more work. I'm assuming that you have review nodes with the ratings and every review node is linked to a business node with a reference field of some sort.

First I would get the nid-s of those review nodes that are linked to the current business node as an array (using fetchCol()) with something like this:

$nids = db_query('
  SELECT entity_id FROM {field_data_field_business_reference}
  WHERE field_business_reference = :nid AND entity_type = :type AND bundle = :bundle',
  array(':nid' =&gt; $entity-&gt;nid, ':type' =&gt; 'node', ':bundle' =&gt; 'review')
)-&gt;fetchCol();


Then get the average:

$entity_field[0]['value'] = db_query('
   SELECT AVG(field_payment_review_value) FROM {field_data_field_payment_review}
   WHERE entity_id IN (:nids) AND entity_type = :type AND bundle = :bundle',
   array(':nids' =&gt; $nids, ':type' =&gt; 'node', ':bundle' =&gt; 'review')
)-&gt;fetchField();


UPDATE:

To avoid the error you're getting you can try wrapping the code inside some if statements to make sure it won't be executed when necessary criteria is not met:

$node = menu_get_object();
if (isset($node-&gt;type) &amp;&amp; $node-&gt;type == 'venue') {
  // put the query that retrieves the nids here.
  if (is_array($nids) &amp;&amp; !empty($nids)) {
    // put the query that calculates the average here.
  }
}


If that's too restrictive you might get away with just this much:

  // put the query that retrieves the nids here.
  if (is_array($nids) &amp;&amp; !empty($nids)) {
    // put the query that calculates the average here.
  }