Have a look at the method Mage_Core_Model_Resource_Db_Collection_Abstract::addExpressionFieldToSelect()
:
/**
* Add attribute expression (SUM, COUNT, etc)
* Example: ('sub_total', 'SUM({{attribute}})', 'revenue')
* Example: ('sub_total', 'SUM({{revenue}})', 'revenue')
* For some functions like SUM use groupByAttribute.
*
* @param string $alias
* @param string $expression
* @param array $fields
* @return Mage_Core_Model_Resource_Db_Collection_Abstract
*/
public function addExpressionFieldToSelect($alias, $expression, $fields)
{
// validate alias
if (!is_array($fields)) {
$fields = array($fields=>$fields);
}
$fullExpression = $expression;
foreach ($fields as $fieldKey=>$fieldItem) {
$fullExpression = str_replace('{{' . $fieldKey . '}}', $fieldItem, $fullExpression);
}
$this->getSelect()->columns(array($alias=>$fullExpression));
return $this;
}
Usage
$collection->addExpressionFieldToSelect("page_view", "sum({{page_view}})", ["page_view"]);
// ^ alias ^ expression ^ used fields
$collection->getSelect()->group("page_view");