<p>Try this:</p>

<pre><code>{# Get this month's data #}
{% set thisMonthStatistic = orders.postDate('and', '&gt;= ' ~ firstDayOfMonth, '&lt;= ' ~ lastDayOfMonth) %}

{# We must set variable "0" and pass it in for the looping's variable #}
{% set thisMonthEarning = 0 %}

{# looping in "thisMonthStatistic". 0 times if none exists #}
{% for statistic in thisMonthStatistic %}

  {# fetch aPrice, bPrice, cPrice, but only if &gt; 0 #}
  {% set aPrice = (statistic.aPrice &gt; 0) ? statistic.aPrice : 0 %}
  {% set bPrice = (statistic.bPrice &gt; 0) ? statistic.bPrice : 0 %}
  {% set cPrice = (statistic.cPrice &gt; 0) ? statistic.cPrice : 0 %}

  {% set thisMonthEarning = thisMonthEarning + aPrice + bPrice + cPrice %}

{% endfor %}

{# Final variable output #}
{{ thisMonthEarning }}
</code></pre>

<p>You don't need the <code>{% if thisMonthStatistic|length %}</code> - if there is no data, you will go through the loop 0 times, and <code>thisMonthEarning</code> will be <code>0</code>.</p>

<p>I am not sure what you intend to do if <code>aPrice</code> is less than 0. My guess is that you want to just skip those, which is what the <code>?:</code> operator is doing here.</p>

<p>Another option (this one includes the prices &lt; 0):</p>

<pre><code>{% set thisMonthStatistic = orders.postDate('and', '&gt;= ' ~ firstDayOfMonth, '&lt;= ' ~ lastDayOfMonth) %}

{% set aPrice = 0 %}
{% set bPrice = 0 %}
{% set cPrice = 0 %}

{% for statistic in thisMonthStatistic %}

  {% set aPrice = aPrice + statistic.aPrice %}
  {% set bPrice = bPrice + statistic.bPrice %}
  {% set cPrice = cPrice + statistic.cPrice %}

{% endfor %}

{{ aPrice + bPrice + cPrice }}
</code></pre>

CC BY-SA 3.0
2016-09-07T19:39:28.717Z
Simple and short. First option win for my needs. Great @Marion Newlevant :)
Dominik Krulak
CC BY-SA 3.0
2016-09-07T18:07:40.010Z
2016-09-07T18:07:40.010Z
Marion Newlevant
<p>Hmm... hard to tell because I can’t know what your expected result would be. But if I had to guess then you might be looking for just this:</p>

<pre><code>{% if aPrice %}
    {% set thisMonthEarning = thisMonthEarning + aPrice %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-09-07T16:19:05.813Z
Hi Carl! Isn't that basically what I have? I need to sum up three different variables at the end. I get correct output If I only set one variable let's say for 'aPrice'. When I add the other one in the way like It's in the question It gets wrong.
Dominik Krulak
CC BY-SA 3.0
2016-09-07T16:30:11.750Z
Yeah, because you add the initial `thisMonthEarning` with every addition again.
carlcs
CC BY-SA 3.0
2016-09-07T16:30:41.053Z
Try what I suggested for each price and get rid of your `{% set thisMonthEarning = aPrice + bPrice + cPrice %}` thingy at the end.
carlcs
CC BY-SA 3.0
2016-09-07T19:36:30.247Z
O yeah, 1st `thisMonthEarning` is passed over to 2nd and (1st + 2nd) over to 3rd `thisMonthEarning` in the loop. That way all three variables get summed up correctly. I like it if I'm right. I vote it up definitely, but @Marion Newlevant 's answer is very close to the one I had at first place and it's shorter in code eventually. From performance perspective I didn't find any down side from both answers. It queries the same count of queries from database for both answers too. So saving document size. Thanks @carlcs again :)
Dominik Krulak
CC BY-SA 3.0
2016-09-07T16:02:32.087Z
2016-09-07T16:02:32.087Z
carlcs
<p>I don't understand what's going on in my code logic because I'm getting unexpected output.</p>

<p>I'd like to sum up three number fields in entries loop for the given post date range. Output gets almost doubled as expected. What am I missing here?</p>

<pre><code>{# Get this month's data #}
{% set thisMonthStatistic = orders.postDate('and', '&gt;= ' ~ firstDayOfMonth, '&lt;= ' ~ lastDayOfMonth) %}

{% if thisMonthStatistic|length %}
{# We must set variable "0" and pass it in for the looping's variable #}
{% set thisMonthEarning = 0 %}

  {# looping in "thisMonthStatistic" #}
  {% for statistic in thisMonthStatistic %}

    {% set aPrice = statistic.aPrice %}
    {% set bPrice = statistic.bPrice %}
    {% set cPrice = statistic.cPrice %}

    {% if aPrice &gt; 0 %}
      {% set aPrice = aPrice + thisMonthEarning %}
    {% endif %}

    {% if bPrice &gt; 0 %}
      {% set bPrice = bPrice + thisMonthEarning %}
    {% endif %}

    {% if cPrice &gt; 0 %}
      {% set cPrice = cPrice + thisMonthEarning %}
    {% endif %}

    {% set thisMonthEarning = aPrice + bPrice + cPrice %}

  {% endfor %}

{% else %}

  {% set thisMonthEarning = 0 %}

{% endif %}


{# Final variable output #}
{{ thisMonthEarning }}
</code></pre>

<number>
Sum up number fields in the loop
# Sum up number fields in the loop

(Asked by: username_0 on 2016-09-07)

I don't understand what's going on in my code logic because I'm getting unexpected output.
I'd like to sum up three number fields in entries loop for the given post date range. Output gets almost doubled as expected. What am I missing here?
{# Get this month's data #}
{% set thisMonthStatistic = orders.postDate('and', '>= ' ~ firstDayOfMonth, '<= ' ~ lastDayOfMonth) %}

{% if thisMonthStatistic|length %}
{# We must set variable "0" and pass it in for the looping's variable #}
{% set thisMonthEarning = 0 %}

  {# looping in "thisMonthStatistic" #}
  {% for statistic in thisMonthStatistic %}

    {% set aPrice = statistic.aPrice %}
    {% set bPrice = statistic.bPrice %}
    {% set cPrice = statistic.cPrice %}

    {% if aPrice > 0 %}
      {% set aPrice = aPrice + thisMonthEarning %}
    {% endif %}

    {% if bPrice > 0 %}
      {% set bPrice = bPrice + thisMonthEarning %}
    {% endif %}

    {% if cPrice > 0 %}
      {% set cPrice = cPrice + thisMonthEarning %}
    {% endif %}

    {% set thisMonthEarning = aPrice + bPrice + cPrice %}

  {% endfor %}

{% else %}

  {% set thisMonthEarning = 0 %}

{% endif %}


{# Final variable output #}
{{ thisMonthEarning }}

**Tags:** number


---

## 2 answers

### Answer by username_1 on 2016-09-07. Score: 2 | Accepted answer

Try this:
{# Get this month's data #}
{% set thisMonthStatistic = orders.postDate('and', '>= ' ~ firstDayOfMonth, '<= ' ~ lastDayOfMonth) %}

{# We must set variable "0" and pass it in for the looping's variable #}
{% set thisMonthEarning = 0 %}

{# looping in "thisMonthStatistic". 0 times if none exists #}
{% for statistic in thisMonthStatistic %}

  {# fetch aPrice, bPrice, cPrice, but only if > 0 #}
  {% set aPrice = (statistic.aPrice > 0) ? statistic.aPrice : 0 %}
  {% set bPrice = (statistic.bPrice > 0) ? statistic.bPrice : 0 %}
  {% set cPrice = (statistic.cPrice > 0) ? statistic.cPrice : 0 %}

  {% set thisMonthEarning = thisMonthEarning + aPrice + bPrice + cPrice %}

{% endfor %}

{# Final variable output #}
{{ thisMonthEarning }}

You don't need the {% if thisMonthStatistic|length %} - if there is no data, you will go through the loop 0 times, and thisMonthEarning will be 0.
I am not sure what you intend to do if aPrice is less than 0. My guess is that you want to just skip those, which is what the ?: operator is doing here.
Another option (this one includes the prices < 0):
{% set thisMonthStatistic = orders.postDate('and', '>= ' ~ firstDayOfMonth, '<= ' ~ lastDayOfMonth) %}

{% set aPrice = 0 %}
{% set bPrice = 0 %}
{% set cPrice = 0 %}

{% for statistic in thisMonthStatistic %}

  {% set aPrice = aPrice + statistic.aPrice %}
  {% set bPrice = bPrice + statistic.bPrice %}
  {% set cPrice = cPrice + statistic.cPrice %}

{% endfor %}

{{ aPrice + bPrice + cPrice }}

> **Comments:**
> **username_0**: Simple and short. First option win for my needs. Great @username_1 :)  

---

### Answer by username_2 on 2016-09-07. Score: 3

Hmm... hard to tell because I can’t know what your expected result would be. But if I had to guess then you might be looking for just this:
{% if aPrice %}
    {% set thisMonthEarning = thisMonthEarning + aPrice %}
{% endif %}

> **Comments:**
> **username_0**: Hi Carl! Isn't that basically what I have? I need to sum up three different variables at the end. I get correct output If I only set one variable let's say for 'aPrice'. When I add the other one in the way like It's in the question It gets wrong.  
> **username_2**: Yeah, because you add the initial `thisMonthEarning` with every addition again.  
> **username_2**: Try what I suggested for each price and get rid of your `{% set thisMonthEarning = aPrice + bPrice + cPrice %}` thingy at the end.  
> **username_0**: O yeah, 1st `thisMonthEarning` is passed over to 2nd and (1st + 2nd) over to 3rd `thisMonthEarning` in the loop. That way all three variables get summed up correctly. I like it if I'm right. I vote it up definitely, but @username_1 's answer is very close to the one I had at first place and it's shorter in code eventually. From performance perspective I didn't find any down side from both answers. It queries the same count of queries from database for both answers too. So saving document size. Thanks @username_2 again :)  

---

