diff --git includes/entity.inc includes/entity.inc
index 5fb36e2..585757e 100644
--- includes/entity.inc
+++ includes/entity.inc
@@ -1177,7 +1177,11 @@ class EntityFieldQuery {
       $select_query->range($this->range['start'], $this->range['length']);
     }
     if ($this->count) {
-      return $select_query->countQuery()->execute()->fetchField();
+      // We cannot use countQuery() here as it will attempt to remove variables
+      // defined in expressions which also appear in WHERE or HAVING clauses.
+      $count_query = db_select($select_query);
+      $count_query->addExpression('COUNT(*)');
+      return $count_query->execute()->fetchField();
     }
     $return = array();
     foreach ($select_query->execute() as $partial_entity) {
