title: Sum of columns from tables in join
body: <p>I have 3 tables, Orders, Orders_products and Orders_total. Currently i have a query that gets the SUM of products for each months, but now we would like to also add the freight cost that is in a different table.</p>
  
  <p>I tried with the following that returned the correct total_value, but the total_shipping is 5 times as big. This i think has to due with that orders, can have multiplie products, but i cant figure out what else to do.</p>
  
  <pre><code>  SELECT Count(DISTINCT O.orders_id)                AS Orders, 
         Sum(OP.final_price * OP.products_quantity) AS total_value, 
         Date_format(O.last_modified, '%m-%Y')      AS date_interval, 
         Sum(OT.value)                              AS total_shipping 
  FROM   
         orders AS O 
  LEFT JOIN 
          orders_total AS OT 
  ON ( OT.orders_id = O.orders_id 
  AND OT.class = 'ot_shipping' ), 
         orders_products AS OP 
  WHERE  
         ( O.orders_id = OP.orders_id ) 
         AND ( O.orders_status = 3 ) 
  GROUP  BY date_interval 
  ORDER  BY O.last_modified DESC 
  </code></pre>
  
  <p>The returned value is:</p>
  
  <pre><code>+----+------------+---------------+----------------+
  | ID | total_value| date_interval | total_shipping |
  +----+------------+---------------+----------------+
  | 17 | 55912.2160 |    01-2014    |      24954     |
  </code></pre>
  
  <p>Expected: </p>
  
  <pre><code>+----+------------+---------------+----------------+
  | ID | total_value| date_interval | total_shipping |
  +----+------------+---------------+----------------+
  | 17 | 55912.2160 |    01-2014    |       4938     |
  </code></pre>
  
  <p>Here is the sqlfiddle <a href="http://sqlfiddle.com/#!2/dfe10/1/0" rel="nofollow">http://sqlfiddle.com/#!2/dfe10/1/0</a></p>
  
  <p>It contains one order, with 3 products in it. the expected total_value is 500 and the expected total_shipping is also 500, but returns 1500 (3 x products). Sadly i had to remove a lot of fields from my table due to a limit in sqlfiddle of max 8000 chars.</p>