Android Question How to select in SQL

manuelsalazar

Member
Licensed User
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
 

edgar_ortiz

Active Member
Licensed User
Longtime User
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

Albert Kallal

Active Member
Licensed User
ok this should work:
This assumes you want invoice document and the amounts EVEN if there are no payments.

B4X:
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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…