Hi all - I am having a nightmare with this query...I have a stock despatch program. A person may place an order for 100 televisions. I may despatch these on 3 seperate despatch notes. On each, I need the amount of stock to follow. If I despatched 10 televisions on the first despatch note, tofollow would be 90.My tables are as follows:OrdersorderidOrderlinesorderlineidproductidqtyDespatchdespatchidDespatchLinesdespatchlineidproductidqtyamountProductsproductidSo far I have the following:SELECT despatch.despatchid, despatch.orderid, despatchline.qty, despatchline.description, products.productcode,[ToFollow] = despatchline.qty - ( SELECT SUM(qty) FROM despatch AS D2 JOIN despatchline AS DL2 ON D2.despatchid = DL2.despatchid AND DL2.productid = products.productid WHERE D2.orderid = orders.orderid )FROM despatch INNER JOIN orders ON despatch.orderid = orders.orderid INNER JOIN clients ON orders.clientid = clients.clientid INNER JOIN despatchline ON despatch.despatchid = despatchline.despatchid INNER JOIN orderline ON orders.orderid = orderline.orderid LEFT OUTER JOIN products ON despatchline.productid = products.productidWHERE (despatch.despatchid = @despatchid)
This is not working - bringing up too many rows and I can't pin down the error... :(Thanks,Steve.