My query is returning incorrect sum of 2 columns from 2 sub tables, i searched over google and also looked the suggestion on stackoverflow but they never worked.
si_invoices
-----------------------------
id, date
1, 2014-05-07
si_invoice_items
-----------------------------
id, invoice_id, date , total
1, 100, 2014-05-07, 200
2, 100, 2014-05-07, 200
si_payment
-----------------------------
id, ac_inv_id, date , payment
1, 100, 2014-05-07, 100
2, 100, 2014-05-07, 200
SELECT SI.*,SUM(SII.total) as total,SUM(SIP.payment) as payment FROM
(SELECT * FROM si_invoices GROUP BY si_invoices.id) AS SI
LEFT JOIN si_invoice_items SII ON SII.invoice_id = SI.id
LEFT JOIN si_payment SIP ON SIP.ac_inv_id = SII.invoice_id
GROUP BY SI.id
It should return 400 sum for field 'total' in sql but it return 800 and same as for 'payment'. Can you please point out what is the error in my query. Please help, appreciated.
Thanks M.S