title: Mysql Group By and Sum Total of the column values
body: <p><a href="https://i.stack.imgur.com/Cd3B3.png" rel="nofollow noreferrer">enter image description here</a> I am having one table like id, sale_id, item_total, tax fields. need to sum the item_total by grouping the tax values.</p>
  
  <h2> Table 1</h2>
  
  <h2>id    |   sale_id  |     item_cost_price   |      tax |</h2>
  
  <h2>1     |      10    |          150          |       5  |</h2>
  
  <h2>2     |      10    |           50          |       7  |</h2>
  
  <h2>3     |       10   |           30          |       5  |</h2>
  
  <p>this is required output:</p>
  
  <hr>
  
  <h2>id    |   sale_id  |     item_cost_price   |      tax |</h2>
  
  <h2>1     |      10    |          180          |       5  |</h2>
  
  <h2>2     |      10    |           50          |       7  |</h2>
  
  <p>When i tried this query, </p>
  
  <pre><code>   SELECT sale_id,tax  FROM bgs_ib_sales_items GROUP BY tax
  
                  $query=$this-&gt;db-&gt;query("SELECT sale_id,tax  FROM bgs_ib_sales_items GROUP BY tax ");
  
              echo $num = $query-&gt;num_rows();
              $result=array();
  
                  foreach($query-&gt;result() as $row){
                       $result_row[]=$row-&gt;sale_id;
                       $result_row[]=$row-&gt;tax;
                       $result_row[]=$row-&gt;item_cost_price;
  
                  }
  </code></pre>
  
  <p>My output is:
  i am getting output like this,</p>
  
  <p>am getting distinct tax only. but i need to sum item total values.</p>
  
  <p>Note:</p>
  
  <p>Image 1 : refer my datatable
   Image 2:  refer my expected output<a href="https://i.stack.imgur.com/BE0Sq.png" rel="nofollow noreferrer">enter image description here</a></p>