Magento 1.9 – Fix Join Query with Group By and Having Error

collection;magento-1.9query

join query with customer table, and group by and having clauses not working, its show an error , filed is not found.

The Query

$collection = $this->getCollection();
        $collection->addAttributeToSelect('userid')->addAttributeToSelect('firstname')->addAttributeToSelect('lastname')->addAttributeToSelect('profile_image')->addAttributeToSelect('specialist')->addAttributeToSelect('latitude')->addAttributeToSelect('free_consult')->addAttributeToSelect('longitude')->addAttributeToFilter('group_id',Federallawyer_Customer_Helper_Data::LAWYER);
 $collection->getSelect()->join(array('review'=>'flrating'),"e.entity_id = review.reviewed_user_id ",array('avg_rating'=>'AVG(review.avg_rating)'));
                $collection->getSelect()->group('review.reviewed_user_id');
$collection->getSelect()->having('avg_rating > ?', $min)

Best Answer

Maybe because this is a calculated field, did you try this for your having clause? :

$collection->getSelect()->having('AVG(review.avg_rating) > ?', $min)