2
I have two tables
Enterprise:
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
Honorary:
id_honorario   tp_honorario  
   5             Mensal                 
   6             Anual
   7             Trimestral
I’m trying to group the Honorary Fee vl_honorario if the fee date is equal to month 12 or if the Fee Type tp_honorario = 'Monthly'.
A sql is 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 get back to 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 because the fee type is Monthly
vl_honorario  id_usuario  
    1100          1                 
    300          212
The monthly fee is added independent of any bond, this?
– Sorack
@Sorack.
– Bia
Your problem is the AND MONTH(h.dt_honorario) = 12 , you only have 2 months with this value
– Marco Souza
@Marconciliosouza is because MONTH(h.dt_honorario) = 12 and if tp_honorario = Monthly
– Bia
In reality its
queryis having some mistakes. YourINNER JOINhas no link and it would limit the number of results. I will try to correct it– Sorack
To start your Join this wrong the relation would be on p.id_honorario= p.id_honorario and in your Where p.tp_honorario = 'Monthly' ...
– Marco Souza
Exactly. It’s in the answer there how would your
query– Sorack