I have four tables, each looks like
ID Value
1 23.43
2 34.23
1 32.23
3 11.25
There can be multiple value entries for each ID within each table. Each ID has values in all four tables.
I want to write a query that gives me the aggregated results per ID. I want to add two of the tables' values and subtract the other two's values from the aggregate.
If were to be writing this in pseudocode:
foreach ID
sumTable1 = sum(Table1.value) where ID=ID
sumTable2 = sum(Table2.value) where ID=ID
sumTable3 = sum(Table3.value) where ID=ID
sumTable4 = sum(Table4.value) where ID=ID
print ID . ": " . sumTable1+sumTable2-sumTable3-sumTable4
Can I do this in a single mySQL query?