I have two tables
Company:
id_empresa id_usuario vl_honorario dt_honorario id_honorario
86 1 200 2017-01-04 7
86 1 600 2016-12-01 6
86 1 500 2016-11-01 5
86 212 300 2016-12-01 5
Fee:
id_honorario tp_honorario
5 Mensal
6 Anual
7 Trimestral
I am attempting to group the amount of the vl_honorario
Fee if the date of the fee is equal to month 12 or if the Type of Fee tp_honorario
= 'Monthly'.
Sql looks like this:
SELECT SUM(h.vl_honorario) as vl_honorario, h.id_usuario
FROM empresa as h
INNER JOIN honorario as p ON p.tp_honorario = 'Mensal'
WHERE h.id_empresa = 86
AND MONTH(h.dt_honorario) = 12
GROUP by h.id_usuario
And it returns me:
vl_honorario id_usuario
600 1
300 212
But I should return the result below, because in 2016-11-01 I have the value of 500, which should be being considered since the type of the fee is Monthly
vl_honorario id_usuario
1100 1
300 212