Help with query in SQLite
Posted: Mon Nov 12, 2018 10:41 am
Hi all,
I put this same query, more than 20 days ago, in https://es.stackoverflow.com/questions/ ... 456_206097 and they have not given me any solution. I put it in this forum because it has to do with the program that I intend to move from FileMaker to LiveCode. If a moderator considers that it is not the right place for this consultation and eliminates it, I will understand it perfectly.
I want to obtain income and expenses from each Property and year. The tables involved are the following: Fincas (Property), Lloguers (Income), Despeses (Expenses).
If I do the two queries separately they work correctly
There is a property that only has expenses
If one the two consultations and filter per year
The revenue totals are multiplied by the number of expense records and vice versa.
Thanks in advance
Carles
I put this same query, more than 20 days ago, in https://es.stackoverflow.com/questions/ ... 456_206097 and they have not given me any solution. I put it in this forum because it has to do with the program that I intend to move from FileMaker to LiveCode. If a moderator considers that it is not the right place for this consultation and eliminates it, I will understand it perfectly.
I want to obtain income and expenses from each Property and year. The tables involved are the following: Fincas (Property), Lloguers (Income), Despeses (Expenses).
If I do the two queries separately they work correctly
Code: Select all
SELECT f.finca Property, l.anyo Year, sum(l.total) Income
FROM Finques f
LEFT JOIN Lloguers l ON l.fincaID = f.fincaID
GROUP BY f.finca, l.anyo
ORDER by f.finca, l.anyo
83 Records. Records and totals OK
Code: Select all
SELECT f.finca Property, d.anyo year, sum(d.total) Expenses
FROM Finques f
LEFT JOIN Despeses d ON f.fincaID = d.fincaID
GROUP BY f.finca, d.anyo
ORDER by f.finca, d.anyo
91 Records. Records and totals OK
If one the two consultations and filter per year
Code: Select all
SELECT f.finca Property, l.anyo Year, sum(l.total) Income, sum(d.total) Expenses
FROM Finques f
LEFT JOIN Lloguers l ON l.fincaID = f.fincaID AND l.anyo = d.anyo
LEFT JOIN Despeses d ON f.fincaID = d.fincaID AND l.anyo = d.anyo
GROUP BY f.finca, l.anyo
ORDER by f.finca, l.anyo
Only 83 recods.
Thanks in advance
Carles