Link to home
Start Free TrialLog in
Avatar of Roslan Ramli
Roslan RamliFlag for Malaysia

asked on

Subsequent question regarding previous successful answer

The question on the link below was answered
https://www.experts-exchange.com/questions/26291124/How-do-I-write-this-query-in-Mysql.html

but now need together with the output, the breakdown of the payments so that with the data results I could process it to produce a statement. I got a query below but not working as expected. It sums up the invoices for all payments and hence we get invoices twice the value if 2 payments are made. Even then it shows only 1 payment.

SELECT
<output name="c.name" title="Name" />,
<output name="c.reference" title="Reference No." />,
<output replace="i.idinvoice" title="Invoice ID" />,
<output replace="(SUM(v.quantity * s.charges))" />AS Invoice,
<output replace="(p.amount)" data="float" />As Amount,
<output name="p.idpayment" title="Payment ID" />,
<output replace="p.cheque_no" title="Cheque No" />,
<output replace="p.pdatetime" title="Processed Time" />,
<output replace="p.user" title="Processed By" type="user" attribute="username" />
FROM inv i
LEFT JOIN invitem v ON v.idinvoice = i.idinvoice
LEFT JOIN service s ON s.idservice = v.idservice
LEFT JOIN payment p ON i.idinvoice = p.idinvoice
LEFT JOIN address a ON a.idaddress = i.idaddress
LEFT JOIN client c ON c.idclient = a.idclient
WHERE a.idaddress = '<input name="idaddress" />'
GROUP BY i.idinvoice
ORDER BY i.idinvoice DESC
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

The fundamental question is, if you have two payments, which payment's data do you want to see, i.e.
idpayment, cheque_no, pdatetime, user from the payment table?

You could try this, but it will arbitrarily give you data from one record in the payment table, with the total payment amount

FROM inv i
LEFT JOIN invitem v ON v.idinvoice = i.idinvoice
LEFT JOIN service s ON s.idservice = v.idservice
LEFT JOIN (select sum(amount) as amount, idpayment, cheque_no, pdatetime, user from payment group by idinvoice) p ON i.idinvoice = p.idinvoice
LEFT JOIN address a ON a.idaddress = i.idaddress
LEFT JOIN client c ON c.idclient = a.idclient
WHERE a.idaddress = '<input name="idaddress" />'
GROUP BY i.idinvoice
ORDER BY i.idinvoice DESC
Correction

FROM inv i
LEFT JOIN invitem v ON v.idinvoice = i.idinvoice
LEFT JOIN service s ON s.idservice = v.idservice
LEFT JOIN (select sum(amount) as amount, idpayment, cheque_no, pdatetime, user, idinvoice
   from payment group by idinvoice) p ON i.idinvoice = p.idinvoice
LEFT JOIN address a ON a.idaddress = i.idaddress
LEFT JOIN client c ON c.idclient = a.idclient
WHERE a.idaddress = '<input name="idaddress" />'
GROUP BY i.idinvoice
ORDER BY i.idinvoice DESC
Avatar of Roslan Ramli

ASKER

I dont want the sum payment. but all payments
which basically means all records, only the invoice value is summed due to possibility of more than one items.
ASKER CERTIFIED SOLUTION
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial