Find and display most recent referencing entities I have 2 different node types. The first one describes a factory and the second one describes a measurement of the water inside the factory. The measurement content type has an Entity Reference field that points to the factory that the measurement is associated with.

What I want to do is print a list of all the factories in a table format and a specific column that will contain a field from the latest measurement that references to the specific factory.

For example:

Factory 1

field 1
field 2
field 3  

Factory 2

field 1
field 2
field 3  

Measurement 1

Refers to Factory 1
Integer Field
Date Created: 21/5/2013  

Measurement 2

Refers to Factory 1
Integer Field
Date Created: 22/5/2013  

Measurement 3

Refers to Factory 2
Integer Field
Date Created: 10/2/2012  

Measurement 4

Refers to Factory 1
Integer Field
Date Created: 23/5/2013  

What I want to print:
    Factory 1 || field 1 || field 2 || field 3 || Integer Field (from Measurement 4)
    Factory 2 || field 1 || field 2 || field 3 || Integer Field (from Measurement 3)

Notice that I'm only printing the Integer field from Measurement 4 since it is the latest measurement for the specific factory. I started writing some custom code and looked into EntityFieldQuery but I can't understand how to use it in conjunction with Entity Reference.
I've made an assumption about the way you want the table to be formatted, but something like the following should get you most of the way there:

// Set up the table.
$header = array('Factory Name', 'Field Value');
$rows = array();

// Get some nids for the factories you're interested in. Your real query will probably be more specific (checking status etc).
$args = array(':type' =&gt; 'factory');
$factory_nids = db_query('SELECT nid FROM {node} WHERE type = :type', $args)-&gt;fetchCol();

// Load the factory nodes.
$factory_nodes = node_load_multiple($factory_nids);

foreach ($factory_nodes as $factory_node) {
  // Add the factory name to the first column of this row.
  $row = array($factory_node-&gt;title);

  // Get the latest measurement related to this factory with an EntityFieldQuery.
  // fieldCondition() and fieldOrderBy()/propertyOrderBy() are the important bits here.
  $query = new EntityFieldQuery();
  $query-&gt;entityCondition('entity_type', 'node')
    -&gt;propertyCondition('type', 'measurement')
    -&gt;propertyCondition('status', 1)
    // This is the entity reference condition; entity ref fields use 'target_id' as the field column name.
    -&gt;fieldCondition('field_factory_reference', 'target_id', $factory_node-&gt;nid)
    // If the date you're interested in is in a field, use something like this...
    -&gt;fieldOrderBy('field_measurement_date', 'value', 'DESC')
    // ...or if you're interested in the node created date, this
    -&gt;propertyOrderBy('created', 'DESC')
    -&gt;range(0, 1);

  $results = $query-&gt;execute();

  // Make sure there are some results.
  if (!empty($results['node'])) {
    // EntityFieldQuerys always return an array of nids even if there's only one result, 
    // so it's easier to run through node_load_multiple and shift the element later
    $measurement_nodes = node_load_multiple(array_keys($results['node']));

    // Get the node
    $latest_measurement_node = array_shift($measurement_nodes);

    if ($field_items = field_get_items('node', $latest_measurement_node, 'field_some_field')) {
      // If we have an appropriate value, add it to the row.
      $row[] = $field_items[0]['value'];
    }
    else {
      $row[] = 'No data found';
    }
  }
  else {
    $row[] = 'No data found';
  }

  $rows[] = $row;
}

$rendered_table = theme('table', array('header' =&gt; $header, 'rows' =&gt; $rows));