M manuelsalazar Member Licensed User Dec 8, 2020 #1 Hi : I have 2 tables. table1 (invoices) with fields : client, invoicedocument, invoiceamount table2 (payments) with fields: client, invoicedocument, payamount (maybe 1 record, or more payments per invoicedocument) i want a SELECT that shows client, invoicedocument, invoiceamount, sum(payamount) thanks in advanced Manuel Salazar
Hi : I have 2 tables. table1 (invoices) with fields : client, invoicedocument, invoiceamount table2 (payments) with fields: client, invoicedocument, payamount (maybe 1 record, or more payments per invoicedocument) i want a SELECT that shows client, invoicedocument, invoiceamount, sum(payamount) thanks in advanced Manuel Salazar
Jmu5667 Well-Known Member Licensed User Longtime User Dec 8, 2020 #2 Hello I suggest you look at this SQL Tutorial (w3schools.com) Regards John. Upvote 0
Bladimir Silva Toro Active Member Licensed User Longtime User Dec 8, 2020 #3 Hi @manuelsalazar You must learn to use the JOIN clause Link:https://www.w3schools.com/sql/sql_join_inner.asp Upvote 0
Hi @manuelsalazar You must learn to use the JOIN clause Link:https://www.w3schools.com/sql/sql_join_inner.asp
E edgar_ortiz Active Member Licensed User Longtime User Dec 9, 2020 #4 You can try: select client, invoicedocument, invoiceamount, ifnull((select sum(payamount) from table2 where table2.client = table1.client and table2.invoicedocument = table1.invoicedocument),0) as total_payamount from table1 Upvote 0
You can try: select client, invoicedocument, invoiceamount, ifnull((select sum(payamount) from table2 where table2.client = table1.client and table2.invoicedocument = table1.invoicedocument),0) as total_payamount from table1
Albert Kallal Active Member Licensed User Dec 9, 2020 #5 ok this should work: This assumes you want invoice document and the amounts EVEN if there are no payments. Code: Copy SELECT Invoices.Client, InvoiceDocument, InvoiceAmount SUM(payamount) as TotalPaid FROM Invoices LEFT JOIN Payments ON Payments.Client = Invoices.Client GROUP BY Invoices.Client, InvoiceDocument, InvoiceAmount Regards, Albert D. Kallal Edmonton, Alberta Canada Upvote 0
ok this should work: This assumes you want invoice document and the amounts EVEN if there are no payments. Code: Copy SELECT Invoices.Client, InvoiceDocument, InvoiceAmount SUM(payamount) as TotalPaid FROM Invoices LEFT JOIN Payments ON Payments.Client = Invoices.Client GROUP BY Invoices.Client, InvoiceDocument, InvoiceAmount Regards, Albert D. Kallal Edmonton, Alberta Canada