Just started an internship and have been tasked with some SQL. Finding it somewhat tricky. Appreciate any help. Thanks!
The task:
“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.”
My proposed logic:
-
First what I want to do is SUM all sales orders, within the next month, for each customer (in the SALES table).
-
Next, SUM all open invoices for each customer, i.e. work out total amount each customer owes (in the INVOICES table).
-
I then want to add the results of 1 and 2 together, by corresponding customer_id
-
Next compare the calculation from 3 above to see whether its greater than each customers credit limit.
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
Cust_ID|Name|Sum_sales_orders (1.above)|Sum_open_invoices (2.above)| Total_orders&invoices
----------------------------------------------------------------------------
1 | A | 25000 | 333 | 25333
| | | |
The code I have so far is
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
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?
I hope you guys understand what I am trying to do. Thanks for the help in advance! 🙂
You can use the HAVING clause to check whether the sum of the totals exceed the credit limit, like so. I’ve also replaced the joins in the WHERE clause with INNER JOINS, for readability sake.
Edit
Just to clarify Dems comment,
GROUP BYdoesn’t allow the specification of aliases, hence the above query ‘repeats’ the sum in theSELECTand in theHAVING. This can be eliminated with nesting, although note that theHAVINGis switched with aWHERE. So a DRYer version of the query is:And, if you prefer, a CTE may make this more readable: