Morning,
really hoping there might be a SQL guru who could give me a little hand, pretty please.
I have 3 tables (simplified).
Users (id, username)
Invoices (id, user_id, due)
Payments (id, user_id, paid)
a user can have invoices with no payments. A user can have multiple payments. Payments do not correspond to invoices.
I want to show a list of users with their due, paid and balance (due-paid).
When I try this with the sum(due), sum(paid) I am getting weird things.
For instance if user.1 has 2 payments of 10.00 and 2 invoices its giving me paid of 40 (guess as its retrieving 4 rows) and doubling up
I have tried various group by options and joins but just cannot get it right
hoping someone can point me in the right direction, I have tried may searches on google and whilst there seem a few “answers” given none seem to actually work
thanks in advance