I have 2 tables Tblinkreceiving and Tblinkdelivery. I want to display the Stock balance for each inkcode.I tried the below sql join query but the calculation is wrong for many inkcodes when i cross check in manual calculation.Where it went wrong ?
select r.inkcode, SUM(r.quantity) Stock-In, SUM(d.quantity) Stock-out, (SUM(r.quantity) - SUM(d.quantity)) Stock-Balance from Tblinkreceiving r,Tblinkdelivery d where r.inkcode=d.inkcode
group by r.inkcode;
You have used an inner join (implicit) to combine receiving and delivery. This means that any ink code that has been received but not delivered will be excluded from your results.
If you can make the assumption that for any ink delivery there must be an ink receipt (based on the premise that you cannot deliver something that you haven't received), then you can use an inner join on tblInkReceiving, as follows:
The left join will return all records on the left (r), including matching records on the right (d). If d has not matching records (i.e. there have been no ink deliveries for that ink code), then the values for those rows will be null.
If you cannot make the assumption that ink can only be delivered once it has been received, then you need to either link to the ink table as well (i.e. the table for which inkcode is the primary key), or union all of the ink codes in the receiving and delivery tables, and link to that: