title: SQL; Sum 2 fields and then add them together
body: <p>Just started an internship and have been tasked with some SQL. Finding it somewhat tricky. Appreciate any help. Thanks!</p>
  
  <p><strong>The task:</strong> </p>
  
  <p>"Would it be possible to write an alert for the following. Show all customers who when Open sales orders due in the next month are combined (added) with open sales invoices, they exceed their credit limit."</p>
  
  <p><strong>My proposed logic:</strong> </p>
  
  <ol>
  <li><p>First what I want to do is SUM all sales orders, within the next month, for each customer (in the SALES table). </p></li>
  <li><p>Next, SUM all open invoices for each customer, i.e. work out total amount each customer owes (in the INVOICES table). </p></li>
  <li><p>I then want to add the results of 1 and 2 together, by corresponding customer_id</p></li>
  <li><p>Next compare the calculation from 3 above to see whether its greater than each customers credit limit.</p></li>
  </ol>
  
  <p>If the summed total in 3 exceeds the credit limit then only these companies should be displayed in the generated table. The ideal format for the resulting table would be</p>
  
  <pre><code>Cust_ID|Name|Sum_sales_orders (1.above)|Sum_open_invoices (2.above)| Total_orders&amp;invoices
  ----------------------------------------------------------------------------
     1   |  A |  25000                   |  333                      | 25333 
         |    |                          |                           |
  </code></pre>
  
  <p>The code I have so far is </p>
  
  <pre><code> SELECT arc.company, arc.credit_limit, 
       sum (ard.unit_price * ard.invoice_qty) as open_invoice_total, 
       sum (od.total_qty_ord * od.unit_price) as open_orders_total
   FROM iqms.arprepost_detail ard, iqms.arprepost arp, iqms.arcusto arc, iqms.ord_detail od, iqms.orders o
   WHERE ard.arprepost_id = arp.id 
       and arc.id = o.arcusto_id and o.id = od.orders_id
       and arp.arcusto_id = arc.id
    GROUP BY arc.company, arc.credit_limit
  </code></pre>
  
  <p>I think that this is calculating the correct totals in the sum () functions? or am i wrong? How can I add open_invoice_total and open_orders_total? and then compare them against the credit_limit? </p>
  
  <p>I hope you guys understand what I am trying to do. Thanks for the help in advance! :)</p>