Ability to SUM() or GROUP BY from the template or ElementCriteriaModel After twitter conversation with Brandon Kelly, is it possible to implement something similar to the following?

craft.entries.section("mySection").sum("myCustomField")
You could do this with a plugin. Create a Variable class with a function called sum(), which accepts an ElementCriteriaModel, passes it to ElementsService::buildElementsQuery(), and then replaces the SELECT value on the resulting DbCommand object:

&lt;?php
namespace Craft;

class FooVariable
{
    public function sum(ElementCriteriaModel $criteria, $column)
    {
        $query = craft()-&gt;elements-&gt;buildElementsQuery($criteria);

        if ($query)
        {
            return $query
                -&gt;select("SUM({$column})")
                -&gt;group(null)
                -&gt;queryScalar();
        }
    }
}


With that in place, you can do this in your templates:

{% set entries = craft.entries.section("mySection") %}
{% set sum = craft.foo.sum(entries, "field_myCustomField") %}


Note that you will need to pass in the field’s full column name (with that “field_” prefix) rather than just its handle. You can figure that out by looking at your craft_content table (or craft_matrixcontent_matrixfieldhandle if it’s a Matrix field). It would have been possible to get the code to be smart enough to figure the column name out for itself, but this way is a bit simpler.