SELECT Cust, QtyIn, QtyOut, QtyIn-QtyOut As QtyDiff FROM ( SELECT Cust, SUM(qin.qty) AS QtyIn, SUM(qout.qty) AS QtyOut FROM Customers cs LEFT JOIN StandsIn qin ON qin.cust = cs.cust LEFT JOIN StandsOut qout ON qout.cust = cs.cust GROUP BY Cust ) dt1 WHERE QtyOut > QtyIn>I am trying to write a SQL Statement that has three tables. I am looking at WIRE STANDS being received from various customers WITHOUT WIRE on them into our Company and a certain number being sent out to customers WITH WIRE on them.
>NAME OF CUSTOMER QTY Recv'd QTY Sent Out Difference (Recv'd-Shipped) >CUSTOMER_A 13 23 -10 >CUSTOMER_B 98 58 40 >CUSTOMER_C 112 140 -28 >CUSTOMER_D 1000 980 20 >>What we are trying to do is to focus on the customers who have not been returning our stands, so that if we see a negative number, we know that that customer has not been returning our WIRE STANDS and can then focus on them.