Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-07T13:15:07.987Z
Answers.Text: 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
Answers.UserDisplayName: nfourtythree
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-06T11:43:16.853Z
Answers.Text: Perfect ! I confirm the `->group(null)` addition to make the total.
Answers.UserDisplayName: migmaker
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-07T13:00:32.983Z
Answers.LastActivityDate: 2017-02-27T19:11:44.743Z
Answers.LastEditDate: 2017-02-27T19:11:44.743Z
Answers.UserDisplayName: Brandon Kelly
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-18T21:14:37.450Z
Answers.Text: I just discovered, if you are summing matrix fields, you need to do this: field_matrixBlockName_fieldName.
Answers.UserDisplayName: 4midori
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-07T17:38:17.887Z
Answers.LastActivityDate: 2019-12-07T17:38:17.887Z
Answers.UserDisplayName: Ottó Radics
Body: <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>
  
Tags: <entries><elementcriteriamodel>
Title: Ability to SUM() or GROUP BY from the template or ElementCriteriaModel
ThreadText: # Ability to SUM() or GROUP BY from the template or ElementCriteriaModel
  
  (Asked by: username_0 on 2014-08-07)
  
  After twitter conversation with username_1, 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](http://buildwithcraft.com/docs/plugins/variables) 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:
  
  ```php
  <?php
  namespace Craft;
  
  class FooVariable
  {
      public function sum(ElementCriteriaModel $criteria, $column)
      {
          $query = craft()->elements->buildElementsQuery($criteria);
  
          if ($query)
          {
              return $query
                  ->select("SUM({$column})")
                  ->group(null)
                  ->queryScalar();
          }
      }
  }
  
  ```
  
  With that in place, you can do this in your templates:
  
  ```twig
  {% 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](https://docs.craftcms.com/v3/dev/element-queries/#query-execution).
  
  > **Comments:**
  > **username_4**: I just discovered, if you are summing matrix fields, you need to do this: field_matrixBlockName_fieldName.  
  
  ---
  
  