<p>You could do this with a plugin. Create a <a href="http://buildwithcraft.com/docs/plugins/variables" rel="nofollow noreferrer">Variable</a> class with a function called <code>sum()</code>, which accepts an ElementCriteriaModel, passes it to ElementsService::buildElementsQuery(), and then replaces the <code>SELECT</code> value on the resulting DbCommand object:</p>

<pre class="lang-php prettyprint-override"><code>&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();
        }
    }
}
</code></pre>

<p>With that in place, you can do this in your templates:</p>

<pre class="lang-twig prettyprint-override"><code>{% set entries = craft.entries.section("mySection") %}
{% set sum = craft.foo.sum(entries, "field_myCustomField") %}
</code></pre>

<p>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.</p>

CC BY-SA 3.0
2014-08-07T13:15:07.987Z
Perfect. The only thing I had to change slightly was the return from `return $query->select("SUM({$column})")->queryScalar();` to `return $query->select("SUM({$column})")->group(null)->queryScalar();` So I could get the complete total for what I needed
nfourtythree
CC BY-SA 3.0
2016-11-06T11:43:16.853Z
Perfect ! I confirm the `->group(null)` addition to make the total.
migmaker
CC BY-SA 3.0
2014-08-07T13:00:32.983Z
2017-02-27T19:11:44.743Z
2017-02-27T19:11:44.743Z
Brandon Kelly
<p>In Craft 3 you can do this without a plugin: </p>

<pre><code>{% set hours = craft.entries
    .section('channelTimetracking')
    .sum('field_myCustomField')
?? null %}
</code></pre>

<p>Details in the <a href="https://docs.craftcms.com/v3/dev/element-queries/#query-execution" rel="noreferrer">docs</a>.</p>

CC BY-SA 4.0
2020-08-18T21:14:37.450Z
I just discovered, if you are summing matrix fields, you need to do this: field_matrixBlockName_fieldName.
4midori
CC BY-SA 4.0
2019-12-07T17:38:17.887Z
2019-12-07T17:38:17.887Z
Ottó Radics
<p>After twitter conversation with Brandon Kelly, is it possible to implement something similar to the following?</p>

<pre><code>craft.entries.section("mySection").sum("myCustomField")
</code></pre>

<entries><elementcriteriamodel>
Ability to SUM() or GROUP BY from the template or ElementCriteriaModel
# Ability to SUM() or GROUP BY from the template or ElementCriteriaModel

(Asked by: username_0 on 2014-08-07)

After twitter conversation with Brandon Kelly, is it possible to implement something similar to the following?
craft.entries.section("mySection").sum("myCustomField")

**Tags:** entries, elementcriteriamodel

---

## 2 answers

### Answer by username_1 on 2014-08-07. Score: 6

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:
elements->buildElementsQuery($criteria);

        if ($query)
        {
            return $query
                ->select("SUM({$column})")
                ->group(null)
                ->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.

> **Comments:**
> **username_0**: Perfect. The only thing I had to change slightly was the return from `return $query->select("SUM({$column})")->queryScalar();` to `return $query->select("SUM({$column})")->group(null)->queryScalar();` So I could get the complete total for what I needed  
> **username_2**: Perfect ! I confirm the `->group(null)` addition to make the total.  

---

### Answer by username_3 on 2019-12-07. Score: 5

In Craft 3 you can do this without a plugin: 
{% set hours = craft.entries
    .section('channelTimetracking')
    .sum('field_myCustomField')
?? null %}

Details in the docs.

> **Comments:**
> **username_4**: I just discovered, if you are summing matrix fields, you need to do this: field_matrixBlockName_fieldName.  

---

