I have 3 table need to join namely table invoice, items and payments. The table items and payments will join to the table Invoice based on invoice id
Now I need to get SUM of sales, sales after cost and payment by invoice date.
So this is my query
SELECT SUM((I.price * qty) - (I.price * qty * discount)) as totalSales,SUM((I.price * qty) - (I.price * qty * discount)-costItem) as salesAfterCost, SUM(PAY.amount) as paymentReceived
FROM tbl_inv B LEFT JOIN tbl_item I ON B.id = I.id_invoice
LEFT JOIN tbl_payment PAY ON B.id = PAY.id_invoice
WHERE B.dateInv = '2013-04-01'
GROUP BY B.id,b.dateInv;
And the result will return like this
totalSales salesAfterCost paymentReceived
75.540000 67.540000 622.8000
But when I check with this query to get total payment, it will return with different value.
SELECT SUM(PAY.amount) paymentReceived
FROM tbl_inv B LEFT JOIN tbl_payment PAY ON B.id = PAY.id_invoice
WHERE B.dateInv = '2013-04-01';
Result :
paymentReceived
155.7000
And query for sales
SELECT SUM((I.price * qty) - (I.price * qty * discount)) as totalSales,SUM((I.price * qty) - (I.price * qty * discount)-costItem) as salesAfterCost
FROM tbl_inv B LEFT JOIN tbl_item I ON B.id = I.id_invoice
WHERE B.dateInv = '2013-04-01';
Result :
totalSales salesAfterCost
37.770000 33.770000
How can I resolved this thing?