0
I have been trying to perform a select that subtracts a column in a table, by another column in another table. See in the image below what I need:
Therefore, I need to add the column quantidade table lista_geral, while the values in the column cod_material and local are equal. Then subtract the result obtained from the table values cautela, that is, while column values cod_material and local, table cautela, are equal to the table lista_geral.
The best result I got was with select below:
SELECT cod_material, (SUM(quantidade) - (SELECT SUM(quantidade)
FROM cautela WHERE local = 'xxx' GROUP BY cod_material)) as total_material
FROM lista_geral WHERE local = 'xxx' GROUP BY cod_material;
However, I did not get the result I need, as this subtracting without restricting the columns cod_material and local.

Can you provide the sql of the tables as well as the sample data mass? So I can try something here in my environment.
– Fábio Jânio
Two subselects aggregating the tables by cod_material , use these selects as virtual tables making a Join on them , just operate the difference.
– Motta
You say you want to add by
cod_materialandlocalbut does GROUP BY just bycod_material. Tnete doGROUP BY cod_material, local.– anonimo