title: CF8 - Sum in a loop
body: <p>I have a query:</p>
  
  <pre><code>&lt;cfquery name="getDesc" datasource="#ds#"&gt;
    SELECT
      desc,
      SUM(charge) as cost,
      COUNT(*) as cnt
    FROM
      product
    WHERE Length(desc) &gt; 0
  &lt;/cfquery&gt;
  </code></pre>
  
  <p>Which then populates a table:</p>
  
  <pre><code>&lt;table&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
          &lt;th&gt;Description&lt;/th&gt;
          &lt;th&gt;Amount of Charges&lt;/th&gt;
          &lt;th&gt;Cost (&amp;pound;)&lt;/th&gt;
        &lt;/tr&gt;
  
        &lt;cfoutput query="getDesc"&gt;
        &lt;tr&gt;
          &lt;td&gt;
            #HTMLEditFormat(getDesc.desc)# &lt;br /&gt;
          &lt;/td&gt;     
          &lt;td&gt;
            #HTMLEditFormat(getDesc.cnt)# &lt;br /&gt;
          &lt;/td&gt;
          &lt;td&gt;
              #HTMLEditFormat(getDesc.cost)# &lt;br /&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
      &lt;/cfoutput&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>My problem is that I'd like to combine two rows of the table that have the same value and also have both of their counts added together.</p>
  
  <p>So far I have:</p>
  
  <pre><code>&lt;table&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
          &lt;th&gt;Description&lt;/th&gt;
          &lt;th&gt;Amount of Charges&lt;/th&gt;
          &lt;th&gt;Cost (&amp;pound;)&lt;/th&gt;
        &lt;/tr&gt;
  
        &lt;cfoutput query="getDesc"&gt;
        &lt;tr&gt;
          &lt;cfif getDesc.desc EQ 'No Charge' OR getDesc.desc EQ 'No Charge (2)'&gt;
            &lt;td&gt;
              No Charge &amp; (2)
            &lt;/td&gt;
            &lt;td&gt;
              &lt;cfset cntSum = arraySum(getDesc['cnt'])&gt;
              #cntSum#
            &lt;/td&gt;
          &lt;cfelse&gt;
          &lt;td&gt;
            #HTMLEditFormat(getDesc.desc)# &lt;br /&gt;
          &lt;/td&gt;     
          &lt;td&gt;
            #HTMLEditFormat(getDesc.cnt)# &lt;br /&gt;
          &lt;/td&gt;
          &lt;/cfif&gt;
          &lt;td&gt;
              #HTMLEditFormat(getDesc.cost)# &lt;br /&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
      &lt;/cfoutput&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>But this gives me two rows of 'No Charge &amp; (2)' and the count is the sum of all the rest of the rows in the table rather than just the two rows I want.</p>
  
  <p>Hope this makes sense.</p>